Carol.Kar
Carol.Kar

Reputation: 5355

Trigger plugin function when pressing button

I am using wordpress 4.9.8 and PHP 7.1.8 I have added a button to the new post screen:

enter image description here

I have added the button via my 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>';
}

I would like to trigger a plugin function:

function updateContent($id) {
        $post = array(
            'ID' => $id,
            'post_content' => "Insert this content"
        );

        // Update the post into the database
        wp_update_post($post);
}

Any suggestion how to do that?

I appreciate your replies!

Update

I implemented the shown answer:

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
}

However, I get the following error when pressing the button:

POST http://localhost/wordpress/wp-admin/admin-ajax.php 400 (Bad Request)

Any suggestions what I am doing wrong?

Upvotes: 0

Views: 938

Answers (1)

kashalo
kashalo

Reputation: 3562

you need to use WordPress Ajax.

here is how you can achieve your desired goal

First your add button code:

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>";

}

Second add our script using admin_footer hook and pass whatever values you want

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
}

Finally your plugin action:

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

Upvotes: 3

Related Questions