How to disable/enable PHP plugin functionality based on a TinyMCE toggle-button

I am currently working on a plugin which sends WP posts to an user-specified REST API, every time a post is published.

The problem: I don’t want to send every post, I’d rather like the user to choose whether to send it, or not (default: don’t send).

So my plugin PHP file looks like this (excerpt):

function post_published_api_call( $ID, $post) {

$url = get_option(‘api_url’, array(‘plugin_text_string’ => DEFAULT_API_URL))[‘plugin_text_string’];
$title = $post->post_title;
$content = wp_post_to_html($post->post_content);

$post_data = array(
‘status’ => ‘publish’,
‘title’ => $title,
‘content’ => $content
);

$json_post = json_encode($post_data);

$data = wp_remote_post($url, array(
‘headers’ => array(‘Content-Type’ => ‘application/json; charset=utf-8’),
‘body’ => $json_post,
‘method’ => ‘POST’
));
}
add_action( ‘publish_post’, ‘post_published_api_call’, 10, 2);

wp_remote_post (or better: the whole post_published_api_call function) should only fire, if a custom button in TinyMCE is pressed/activated.

My Custom TinyMCE Editor Button (pb_button.js):

(function() {

tinymce.create(‘tinymce.plugins.pboerse’, {

init : function(ed, url) {
var state;

ed.addButton(‘pb_button1’, {
text : ‘PB’,
title : ‘Publish on Projektboerse?’,
cmd : ‘pb_button1’,

onclick: function () {

},

onpostrender: function() {
var btn = this;
ed.on(‘pb_button1’, function(e) {
btn.active(e.state);
});
}
});

ed.addCommand(‘pb_button1’, function() {

state = !state; /* Switching state */
ed.fire(‘pb_button1’, {state: state});

if (state){
/* Button active */
var request = new XMLHttpRequest();
request.open(‘POST’, ‘projektboerse.php’, true);
request.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
request.send(‘mydata=’+state);
}
else {
/* Button inactive */
}

});

}
});

// Register plugin
tinymce.PluginManager.add( ‘pboerse’, tinymce.plugins.pboerse );
})();

I know about the difficulties in exchanging states/variables between a server side language like PHP and a client side language like JavaScript.

I googled a lot about AJAX but a problem remains: I can get the button state via the following code

foo = isset($_POST[‘mydata’]);

if (isset($_POST[‘mydata’])){
$GLOBALS[‘foo’] = $_POST[‘mydata’];
echo $foo;
wp_die();
}

but the button state only lives inside the if statement because I guess the rest of the plugin still got the initial default value (which is an empty string).

I tried using the if-block inside the post_published_api_call function, but at this time the $_POST variable seems to be overwritten by wordpress, and so $_POST[‘mydata’] results in an empty string.

How can I check for the button state inside the post_published_api_call function? Is it possible?

Thanks in advance!

Read more here:: How to disable/enable PHP plugin functionality based on a TinyMCE toggle-button

Leave a Reply

Your email address will not be published. Required fields are marked *