How to get post from pure frontend AJAX (using only post ID)?

You can output your javascript via wp_add_inline_script(). This way you can set the post”s id and AJAX URL before outputting the code: wp_add_inline_script(‘my-js’, ‘ jQuery.ajax({ \’url\’ : ‘.admin_url(‘admin-ajax.php?action=my_action’).’ data : { \’id\’ : ‘.get_the_ID().’ } });’); Note that you need to hook to an existing js file to be able to add inline script, so …

Read more

why does not work ajax that add_action registered in wordpress

The AJAX request is a separate request from the one that renders your page. Your AJAX handler is getting registered on the request that serves the page, but not the AJAX request, which is the one that matters. Move your add_action and function to your theme’s functions.php file, which loads on all requests.

Front end theme options ajax returns 0

You’re sending a bad action for your AJAX request, the action should match with your string next to wp_ajax_: add_action(‘wp_ajax_fend_theme_data_save’, ‘fend_theme_data_save’);, so your action HTML field should be like this: <input type=”hidden” name=”action” value=”fend_theme_data_save” /> See the difference? Your action value should always be next to wp_ajax_ or wp_ajax_nopriv, whatever you’re using. I recommend you …

Read more

Send checkbox status with Ajax / JSON and save it

.prop() method return boolean type. True or false. You should send checked value and save it into database. And in HTML get your value and set it to your checkbox. An example you have <input type=”checkbox” name=”test” class=”checkbox” value=”checked”>. You should send to php $(‘.checkbox’).val(); and then place checked attr to html. <?php $checked = …

Read more

Need help with ajax

The WordPress Plugin Developer Handbook has background info and code samples of how to use Ajax in your plugin (and themes). Give it a read and it should get you on the right path.

Full Front End, AJAX comment system and comment reply script

<script> var addComment = { settings: function( parentId, postId ) { var t = $(this), commentDivName = $(‘#comment-‘+parentId), formDivName = $(‘#commentForm-‘+postId), formReplyDivName = $(‘#commentReplyForm-‘+parentId), postDivName = $(‘#post-‘+postId), theform = formDivName.find(‘form.comment-form’); if( !t.length || !commentDivName.length || !formDivName.length || !postDivName.length ) { return; } if(!theform.length){ $(postDivName).find(‘[id^=commentReplyForm-]’).each(function(){ if($(this).find(‘form.comment-form’).length){ formDivName = $(‘#’+$(this).attr(‘id’)); } }); theform = formDivName.find(‘form.comment-form’); } theform.find(‘input[name=parent]’).val(parentId); …

Read more