douji0073 2018-02-16 16:57
浏览 86
已采纳

在WordPress插件中将变量从Ajax传递到PHP

I am developing a WordPress plugin and I am trying to pass a variable from ajax to a php file. Both files are inside my plugin folder. The js file is running but when I fire the ajax function, it seems that is not sending a post.

Plugin structure:

  • -plugin folder

    --ajax.js

    --folder/example.php

This is my ajax.js

// using jQuery ajax
// send the text with PHP
$.ajax({
        type: "POST",
        url: "/absoluteurlpluginfolder/folder/example.php",
        data: {
            'action': 'my_action',
            'whatever': 1234
                },
            // dataType: "text",
            success: function(data){
              console.log('Connection success.');
              // console.log(data);
            }
          });

And this is my example.php

add_action( 'wp_ajax_my_action', 'my_action' );

function my_action() {
    global $wpdb; // this is how you get access to the database

    $whatever = intval( $_POST['whatever'] );

    $whatever += 10;

  alert($whatever);

    wp_die(); // this is required to terminate immediately and return a proper response
}

I have two problems:

  1. I cannot see that example.php is receiving anything
  2. How could I use a relative URL to connect with my PHP file? When I try such as 'url: "folder/example.php",' it seems that it starts with "http://localhost/my-wp-project/wp-admin/" and not in my plugin folder, and fails.
  • 写回答

2条回答 默认 最新

  • drnycqxwz63508434 2018-02-18 15:31
    关注

    I think that the main problem was that I need to add "wp_enqueue_script" and "wp_localize_script". However, I am working in the development of a TinyMCE plugin inside WordPress.

    That means that although the JS file is already include, it is not working when I add "wp_enqueue_script" and "wp_localize_script". Why? I do not know but the strange thing is that I made it working with another line.

      wp_register_script( 'linked-plugin-script', null);
    

    I have tried with different versions, and the minimum necessary to work is this one above. I can put the URL, version, jquery dependency and false or true. All of them work.

    So at the end this is my code and is working. This is the plugin.php

    // Include the JS for TinyMCE
    function linked_tinymce_plugin( $plugin_array ) {
        $plugin_array['linked'] = plugins_url( '/public/js/tinymce/plugins/linked/plugin.js',__FILE__ );
        return $plugin_array;
    }
    
    // Add the button key for address via JS
    function linked_tinymce_button( $buttons ) {
        array_push( $buttons, 'linked_button_key' );
        return $buttons;
    }
    
    
    // Enqueue the plugin to manage data via AJAX
    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    function my_enqueue() {
    
      wp_register_script( 'linked-plugin-script', null);
    
      wp_enqueue_script( 'linked-plugin-script');
    
        // in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
        wp_localize_script( 'linked-plugin-script', 'ajax_object', array(
                                                            'ajax_url' => admin_url( 'admin-ajax.php' ),
                                                            'whatever' => '' )
                                                          );
    }
    
    
    // Same handler function...
    add_action( 'wp_ajax_my_action', 'my_action' );
      function my_action() {
        global $wpdb;
    
        $whatever = strtoupper($_POST['whatever']);
        echo $whatever;
    
        wp_die();
    }
    

    And this is the plugin of TinyMCE in JavaScript

    // JavaScript file for TinyMCE Linked Plugin
    //
    //
    //
    ( function() {
        tinymce.PluginManager.add( 'linked', function( editor, url ) {
    
            // Add a button that opens a window
            editor.addButton( 'linked_button_key', {
                // Button name and icon
                text: 'Semantic Notation',
                icon: false,
                // Button fnctionality
                onclick: function() {
    
                  // get raw text to variable content
                  var content = tinymce.activeEditor.getContent({format: 'text'});
    
                  // using jQuery ajax
                  // send the text to textrazor API with PHP
                  $.ajax({
                    type: 'POST',
                    url: ajax_object.ajax_url,
                    data: { 'action': 'my_action',
                              'whatever': ajax_object.whatever = content
                           },
                    beforeSend: function() {
                      console.log('before send..');
                     },
                    success: function(response){
                      console.log('Success the result is '+response);
                    }
                  });
    
    
    
    
    
    
    
    
                } // onclick function
    
            } ); // TinyMCE button
    
        } ); // tinymce.PluginManager
    
    } )(); // function
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题