• Resolved egeo

    (@egeo)


    Hello all,

    Is there a way to add a json-POST button into wp admin bar. Up to now I ve found methods like add_menu and add_node but I still havent found a way to add the POST code somewhere as an argument.

    Any help is really appreciated – thank you in advance for your time

Viewing 1 replies (of 1 total)
  • Thread Starter egeo

    (@egeo)

    I found a way:

    I used a plugin to set the button on the upper part of wp-admin page with add_node($args). Used the function ‘wp_enqueue_script’ and hook ‘admin_enqueue_scripts’. Then added a js folder within my plugin file containing the appropriate js file to take the input of a hypothetical form and post it.

    PHP FUNCTIONS

    function my_jsonpost_script() {
    	wp_enqueue_script(
    		'submitform',
    		plugins_url() . '/admin-button-plugin/js/jsonpost.js',
    		array( 'jquery' )
    	);
    }
    
    add_action( 'admin_enqueue_scripts', 'my_jsonpost_script' );
    
    function custom_button_example($wp_admin_bar){
    $args = array(
    'id' => 'custom-button',
    'title' => 'Custom Button',
    'href' => '#',
    'meta' => array(
    'class' => 'custom-button-class'
    )
    );
    $wp_admin_bar->add_node($args);

    JS FILE

    (function($) {
      $(document).ready(function(){
    
        $("a.ab-item").click(function(){
    
    	var url = '/users/' + $('#user_id').val();
        $('#myform').attr('action', url);
        var data = JSON.stringify({
            "userdata": $('#user_data').val()
        })
        $('<input type="hidden" name="json"/>').val(data).appendTo('#myform');
        $("#myform").submit();
    
          window.alert(data);
          return false;
        })
     });
    }(jQuery));
Viewing 1 replies (of 1 total)

The topic ‘Adding a button in admin bar for POST-ing json data’ is closed to new replies.