Access the environment of an admin page from another admin page

Ok, this could be achieved this way, probably. add_action(‘add_meta_boxes_my_super_duper_post_type’, ‘get_metabox_global_ajax’, 9999 ); function get_metabox_global_ajax(){ if( isset($_GET[‘mgv’]) && $_GET[‘mgv’] == ‘1’ ){ global $wp_meta_boxes; @error_reporting( 0 ); header( ‘Content-type: application/json’ ); die( json_encode( $wp_meta_boxes )); } } Now, you send a request from your plugin page to post-new.php?post_type=my_super_duper_post_type&mgv=1 through ajax, and use the returned json response … Read more

admin-ajax.php vs .load() in WordPress

Is it the only way to ajaxify your whole site or if there is any better way? No, it’s not the only way. As you already know, you can indeed use .load(). As to what’s better? That’s wide open to debate and depends on personal preference, requirements, circumstances etc. Cannot we use .load() method with … Read more

Fetch and show multiple custom fields via AJAX

To pass data from js back to PHP simplest way is use json_encode or, in WP, its wrappers wp_send_json, wp_send_json_error and wp_send_json_success. Usage example: On PHP side function get_latest_product_meta() { $post_id = (int) filter_input(INPUT_GET, ‘post_id’, FILTER_SANITIZE_NUMBER_INT); $post_id or wp_send_json_error(); // array_shift because when used like so `get_post_meta` return array of arrays… $data = array_map(‘array_shift’, get_post_meta($post_id)); … Read more

How do I send an ajax request and get a response back from a function?

$.ajax is very simple and you can append more parameters easily in the data: {action:’my_test_ajax’} line, try this: var target=”http://” + window.location.hostname + ‘/wp-admin/admin-ajax.php’; $.ajax({ url: ajax, data: {action:’my_test_ajax’}, type: ‘post’, success: function(data){ console.log(data, data.title, data.content) } }); Make sure the target points exactly to admin-ajax.php or set a global variable (or using localize script) … Read more

Load more AJAX on WP Query

if you want to use ajax in wordpress you must have to follow these step: 1) first make your js file like main.js. 2) localize it in functions.php with wp_localize_script(). wp_enqueue_script( ‘main.js’, get_template_directory_uri().’/js/main.js’, array( ‘jquery’ ) ); wp_localize_script( ‘main.js’, ‘ajax_object’, array(‘ajax_url’ => admin_url(‘admin-ajax.php’)) ); 3) make your javascript or jquery code to run your query … Read more

Redirect to Page after Post Submit

You can’t. If you wish to redirect a user by PHP, you should set the response header. If you output any data, you will not be able to, and you will get a Header already sent error. Look at this line: wp_redirect( home_url(‘/?page_id=7’) ); exit; echo $result; die(); When you use exit ( which is … Read more

wp_localize_script $handle

It’s basically a unique id of the script you registered or enqueued before. Let’s say we enqueued a two scripts with wp_enqueue_script(): wp_enqueue_script( ‘my_script_1′,’/js/some_script.js’ ); wp_enqueue_script( ‘my_script_2′,’/js/some_other_script.js’ ); Now you want to pass your $data to the script #2 with wp_localize_script(): wp_localize_script( ‘my_script_2’, ‘my_script_2_local’, $data ); After this – when WordPress prints out your my_script_2 … Read more