php - 按下按钮时触发插件功能
问题描述
我正在使用wordpress 4.9.8
并且PHP 7.1.8
我在新的帖子屏幕上添加了一个按钮:
我已经通过我的添加了按钮function.php
:
add_action('media_buttons', 'add_my_media_button', 99);
function add_my_media_button()
{
echo '<a href="#" id="insert-my-media" class="button">Own content</a>';
}
我想触发一个插件功能:
function updateContent($id) {
$post = array(
'ID' => $id,
'post_content' => "Insert this content"
);
// Update the post into the database
wp_update_post($post);
}
任何建议如何做到这一点?
感谢您的回复!
更新
我实现了显示的答案:
add_action( 'media_buttons', 'add_my_media_button', 99 );
function add_my_media_button() {
$post = $GLOBALS['post_ID'];
echo "<a href='#' id='insert-my-media' data-post-id='{$post}' class='button'>Own content</a>";
}
add_action( 'wp_ajax_my_action', 'updateContent' );
function updateContent() {
$post_id = intval( $_POST['post_id'] );
wp_die(); // this is required to terminate immediately and return a proper response
$post = array(
'ID' => $post_id,
'post_content' => 'Insert this content',
);
// Update the post into the database
wp_update_post( $post );
}
add_action( 'admin_footer', 'my_media_button_script' );
function my_media_button_script() {
?>
<script>
jQuery(document).ready(function ($) {
$('#insert-my-media').click(function () {
var post_id = $(this).attr('data-post-id');
var data = {
'action': 'updateContent',
'post_id': post_id
};
console.log("test: " + ajaxurl)
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function (response) {
alert('Got this from the server: ' + response);
});
});
});
</script>
<?php
}
但是,按下按钮时出现以下错误:
POST http://localhost/wordpress/wp-admin/admin-ajax.php 400(错误请求)
有什么建议我做错了吗?
解决方案
你需要使用 WordPress Ajax。
这是您实现预期目标的方法
首先你的添加按钮代码:
add_action( 'media_buttons', 'add_my_media_button', 99 );
function add_my_media_button() {
$post = $GLOBALS['post_ID'];
echo "<a href='#' id='insert-my-media' data-post-id='{$post}' class='button'>Own content</a>";
}
其次使用admin_footer
钩子添加我们的脚本并传递您想要的任何值
add_action( 'admin_footer', 'my_media_button_script' );
function my_media_button_script() {
?>
<script>
jQuery(document).ready(function ($) {
$('#insert-my-media').click(function () {
var post_id = $(this).attr('data-post-id');
var data = {
'action': 'updateContent',
'post_id': post_id
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function (response) {
console.log( response ) ;
});
});
});
</script>
<?php
}
最后你的插件操作:
add_action( 'wp_ajax_updateContent', 'updateContent' );
function updateContent() {
$post_id = intval( $_POST['post_id'] );
$post = array(
'ID' => $post_id,
'post_content' => 'Insert this content',
);
// Update the post into the database
if ( wp_update_post( $post ) ) {
echo 'Updated';
};
wp_die(); // this is required to terminate immediately and return a proper response
}
推荐阅读
- r - 在R中的多个数据帧的函数中结合for循环和变异
- php - PHP:缩进文件,块由 { 和 } 分隔
- kubernetes - 如何使用 Kubernetes 自定义资源作为部署配置?
- r - R:基于字符串替换列值的有效方法(可能使用 case_when 或其他形式的变异)?
- sdk - 如何将包从构建机器移动到 SDK/交叉编译器工具链
- c# - 检测用户在 Internet Explorer 加载项 dll 上的下载
- javascript - 如何使用 animated.css 一次在两个元素上应用动画
- javascript - 隐藏特定的菜单项
- node.js - 构建和部署 Node.js TypeScript 服务器的生产版本
- weblogic - 如何使用 systemd 管理 Weblogic 12c 组件?