Favouriting a Custom Post Type

You should not be using element IDs more than once, instead you should be using classes. Without going into to much detail try the adjustments below… Change the following: <a href=”#add-to-favorite” id=”add-to-favorite”> <i class=”fa fa-star-o”></i>&nbsp; <span class=”add-to”>Add to Favourites</span> </a> …to <a href=”#add-to-favorite” class=”add-to-favorite”> <i class=”fa fa-star-o”></i>&nbsp; <span class=”add-to”>Add to Favourites</span> </a> Change your JS … Read more

Allow only new sub-pages to be created

you actually don’t need any ajax or server side action, simple see if the user has selected a parent page: add_action( ‘admin_head-post-new.php’, ‘publish_admin_hook_wpse_78690’ ); add_action( ‘admin_head-post.php’, ‘publish_admin_hook_wpse_78690’ ); function publish_admin_hook_wpse_78690() { global $current_screen; if( ‘page’ != $current_screen->post_type ) return; ?> <script language=”javascript” type=”text/javascript”> jQuery(document).ready(function() { jQuery(‘#publish’).click(function() { var parent_id = jQuery(‘#parent_id’).val(); if (parseInt(parent_id) > 0){ … Read more

Is it safe to assume that a nonce may be validated more than once?

1, the nonce lifetime is about 24 hours by default actually. take a look at wp_verify_nonce function. To be more accurate, the lifetime is controlled by filter apply_filters( ‘nonce_life’, DAY_IN_SECONDS ); 2, if the lifetime value makes you doubt if it is “an implementation side-effect”, you may want to add_filter(‘nonce_life’,create_function(‘$v’, ‘return 60*5;’)); to shorten the … Read more

username_exists() function can’t be access without logging in

When using Ajax API, and you want to make the ajax callback available for non-logged users, you need to add 2 actions, “wp_ajax_{$action}” and “wp_ajax_nopriv_{$action}”. Using only the first action, the callback will be called only for logged users, using only the second it will be called only for non-logged visitors. Try this: function check_username() … Read more

Call to undefined function add_action()

WordPress has a native AJAX file that should do all that you want, and this will ensure that all native WP functions are included. If you require additional functionality from ‘framework/AsyncAction.php’, you can include_once(), as you do with ‘SettingsController.php’. See the Codex for more information and examples – AJAX in Plugins If you wish to … Read more

Retrieve POST data from AJAX call

Problem “Serialization” is the act of turning a data-object into a string-representation. jQuery automatically serializes the data property before sending an AJAX request. The server then deserializes the GET querystring from the URL, and the request body for POST requests before populating PHP’s request variables. Your code functioned as expected when data consisted solely of … Read more

WordPress Ajax URL for function in functions.php

//ajax call var data = { action: ‘folder_contents’, path: datafolder }; var ajaxurl = baseurl + ”; //WHAT IS THIS?!?! jQuery.post(ajaxurl, data, function(response) { alert(‘Got this from the server: ‘ + response); console.log(response); }); And your PHP in functions.php //Called by Ajax from App – list the contents of the folder so they can be … Read more