首页 > 解决方案 > 按下按钮时触发插件功能

问题描述

我正在使用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(错误请求)

有什么建议我做错了吗?

标签: phpwordpress

解决方案


你需要使用 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
}

参考


推荐阅读