按下按钮时触发插件功能

时间:2018-09-16 08:12:26

标签: php wordpress

我正在使用wordpress 4.9.8PHP 7.1.8在新的帖子屏幕上添加了一个按钮:

enter image description here

我已经通过我的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(错误   请求)

有人建议我在做什么错吗?

1 个答案:

答案 0 :(得分:2)

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

Reference