猿问

我的第一个基本 wordpress 插件的问题

我正在尝试创建我的第一个 worpress 插件!


实际上,这个想法是当我点击按钮时,一个 ajax 请求被发送到 php 文件文件(ajax-process.php),它包含一个非常基本的代码,用于从数据库中提取一些数据,然后将其显示为警报或其他在我的主页。


这是我的插件 floder(在 wordpress 插件文件夹中)


DB-拉拔器:


   - DB-Puller.php

   - ajax-process.php

和 js (js_file.js) + css (css_file.css) 文件夹。


这里包含 DB-Puller.php



<?php

/**

 * Plugin Name: DB-Puller 

 * Plugin URI: https://my-web-site.com/

 * Description: This is a my firt plugin, it(s allows to display data from database. 

 * Version: 0.1

 * Author: Firest Last name

 * Author URI: https://my-web-site.com/

 * License: GPL3

 */


function scripts_files_enqueue_scripts() {


// Adding css file    

wp_enqueue_style('css_file',plugins_url( 'css/css_file.css', __FILE__ ) );


// Adding JS file    

wp_enqueue_script( 'js_file', plugins_url( 'js/js_file.js', __FILE__ ), array('jquery'), '1.0', true );

}



add_action('wp_enqueue_scripts', 'scripts_files_enqueue_scripts');

?>


这包含 ajax-process.php

注意:数据库表非常基本,它只包含 id + text 列


<?php


function my_ajax_action_callback()

{

   if (isset($_POST['req']))

        {

            global $wpdb;

            $quer = $wpdb->get_results( "SELECT * FROM wp_custom_table_1" ); 

            $arr = $quer[0]->text;

            echo $arr;

            die();

        }

    wp_die(); // required. to end AJAX request.

}



什么包含js文件


jQuery(function($){


    $('body').prepend('<button class="btn" type="button">PULL DATA</button>');


    $('button.btn').on('click', function()

                  {


        $.ajax({


            url:'http://127.0.0.1/wp522/wp-content/plugins/DB-Puller/ajax-process.php',

            method:'POST',

            data:{

                  req:'',

                  action:'my_ajax_action',

                  },

            success:function(data)

            {

                alert(data);

            },

            error:function()

            {

                alert(erooor);

            }

        })

    })

})

警报发送为空!请帮我检测问题出在哪里!


谢谢你。


12345678_0001
浏览 153回答 1
1回答

呼唤远方

查看代码,它似乎不像 Wordpress 做这种事情的方式。首先,您需要将您ajax-process.php的插件包含在主文件中,例如:require_once plugin_dir_path(__FILE__) . '/ajax-process.php';其次,你需要像这样注册你的ajax回调:add_action('wp_ajax_my_ajax_action', 'my_ajax_function');add_action('wp_ajax_no_priv_my_ajax_action', 'my_ajax_function');然后在其中注册 ajaxUrl,scripts_files_enqueue_scripts()以便可以从您的 javascript 访问它。该admin-ajax.php文件处理所有 ajax 请求:wp_localize_script(&nbsp; &nbsp;'js_file',&nbsp; &nbsp;'ajax',&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; 'ajaxUrl' => admin_url('admin-ajax.php'),&nbsp; &nbsp; ));然后在您的 javascript 中,您需要使用ajaxUrl并指定 ,action这将告诉 Wordpress 应触发哪个回调:jQuery(function($) {&nbsp; $('body').prepend('<button class="btn" type="button">PULL DATA</button>');&nbsp; $('button.btn').on('click', function() {&nbsp; &nbsp; $.post({&nbsp; &nbsp; &nbsp; &nbsp; url: ajax.ajaxUrl,&nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req: '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action: 'my_ajax_action',&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(data);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('error');&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});这是一篇很好的文章AJAX in Plugins,解释了如何在插件中使用ajax。
随时随地看视频慕课网APP
我要回答