在 WordPress 插件中将变量从 Ajax 传递到 PHP
Passing variable from Ajax to PHP in WordPress plugin
我正在开发一个 WordPress 插件,我正在尝试将变量从 ajax 传递到 php 文件。这两个文件都在我的插件文件夹中。 js 文件是 运行,但是当我触发 ajax 函数时,它似乎没有发送 post。
插件结构:
-插件文件夹
--ajax.js
--folder/example.php
这是我的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);
}
});
这是我的 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
}
我有两个问题:
- 我看不到 example.php 正在接收任何东西
- 如何使用亲戚 URL 连接我的 PHP 文件?当我尝试 'url: "folder/example.php",' 时,它似乎以“http://localhost/my-wp-project/wp-admin/”开头,而不是在我的插件文件夹中,并且失败了。
你见过this page吗?这是最好的教程。但是你错过了一些东西:
您应该使用 wp_localize_script()
函数设置全局 js 变量。喜欢
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
将JS中的url替换为ajax_object.ajax_url
。
如果您想使用 ajax 使用 wp_ajax
挂钩 - 您应该发送所有请求 wp-admin/admin-ajax.php
。你可以通过 admin_url('admin-ajax.php');
.
得到这个 url
我认为主要问题是我需要添加 "wp_enqueue_script" 和 "wp_localize_script"。但是,我正在开发 WordPress 中的 TinyMCE 插件。
也就是说虽然已经包含了JS文件,但是当我添加"wp_enqueue_script"和"wp_localize_script"时,它不起作用。为什么?我不知道,但奇怪的是我让它与另一条线一起工作。
wp_register_script( 'linked-plugin-script', null);
我试过不同的版本,最低要求是上面这个。我可以输入 URL、版本、jquery 依赖项和 false 或 true。他们都工作。
所以最后这是我的代码并且正在运行。
这是 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();
}
这是JavaScript
中TinyMCE的插件
// 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
我正在开发一个 WordPress 插件,我正在尝试将变量从 ajax 传递到 php 文件。这两个文件都在我的插件文件夹中。 js 文件是 运行,但是当我触发 ajax 函数时,它似乎没有发送 post。
插件结构:
-插件文件夹
--ajax.js
--folder/example.php
这是我的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);
}
});
这是我的 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
}
我有两个问题:
- 我看不到 example.php 正在接收任何东西
- 如何使用亲戚 URL 连接我的 PHP 文件?当我尝试 'url: "folder/example.php",' 时,它似乎以“http://localhost/my-wp-project/wp-admin/”开头,而不是在我的插件文件夹中,并且失败了。
你见过this page吗?这是最好的教程。但是你错过了一些东西:
您应该使用
wp_localize_script()
函数设置全局 js 变量。喜欢wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
将JS中的url替换为
ajax_object.ajax_url
。
如果您想使用 ajax 使用 wp_ajax
挂钩 - 您应该发送所有请求 wp-admin/admin-ajax.php
。你可以通过 admin_url('admin-ajax.php');
.
我认为主要问题是我需要添加 "wp_enqueue_script" 和 "wp_localize_script"。但是,我正在开发 WordPress 中的 TinyMCE 插件。
也就是说虽然已经包含了JS文件,但是当我添加"wp_enqueue_script"和"wp_localize_script"时,它不起作用。为什么?我不知道,但奇怪的是我让它与另一条线一起工作。
wp_register_script( 'linked-plugin-script', null);
我试过不同的版本,最低要求是上面这个。我可以输入 URL、版本、jquery 依赖项和 false 或 true。他们都工作。
所以最后这是我的代码并且正在运行。 这是 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();
}
这是JavaScript
中TinyMCE的插件// 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