add_action( ‘publish_post’,function) giving “Cannot modify header” error
-
Problem –
We have a prototype WordPress plugin for audit of news article quotes. It has a button named “Get Source Diversity”(GSD) on post page for classic editor. This goes to a rest api call and this functionality already works. The plugin gets the a response from an external server and displays results. Our goal now is to hook the action being called on “Get source Diversity” button click to “Publish” button click. I.e. A deployed site is asking us to automate the GSD action when a post is published.
I am using the publish_post hook in this way in the plugin code-
add_action( ‘publish_post’, ‘my_action_javascript’ );my_action_javascript is my function in plugin file which I want hook the publish_post action to as this is the function which is called when “Get Source Diversity” button is clicked.
This function calls another function(my_action) through ajax like this where a REST API call is written-
var post_data = {
‘action’: ‘my_action’,
security: ‘<?php echo esc_js($ajax_nonce); ?>’,
‘postData’: jsonObject
};
jQuery.ajax({
url: ajaxurl,
type: ‘post’,
data: post_data
})I am getting this error when I use this hook where I am not able resolve this.
Cannot Modify Header Information – headers already sent by (file path)Is my invocation of the publish post hook correct?
I’d greatly appreciate any inputs. What is needed is that when the post is published, the usual WP publish action must action AND our Get Source Diversity button action (that runs when the button is pressed) must also run after that.
- The topic ‘add_action( ‘publish_post’,function) giving “Cannot modify header” error’ is closed to new replies.