Wordpress Plugin - Embed Javascript into PHP Function

Wednesday, November 4, 2015

I am trying to create a Wordpress plugin by GCM service which is provided by Google.



I read quite a lot of article about creating web service as a plugins.



After doing some research and reading, I managed to created GCM Web Service.



It works like a charm.



<?php
require ('gcm-web-service.php');

add_action('admin_menu', 'gcm_push_javascript');
add_action('wp_ajax_gcm_push', 'gcm_push_action');

function gcm_push_javascript() {
?>
https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js

jQuery(document).ready(function($) {
$("#push-promotion").click(function() {

$(this).find(".spinner").addClass("is-active");

var id = $(this).data("id");

var data = {
'action': 'gcm_push',
'post_id': id
};


$.ajax({
type: "POST",
url: ajaxurl,
data: data,
success: function(response) {

$(".publish-to-phone #publishing-action .spinner").addClass('is-active');
}
}).done(function() {
$(".publish-to-phone #publishing-action .spinner").removeClass('is-active');

$('
Promotion had been pushed to mobile successfully.
').prependTo('body');

$n = $('#notification'),
nHeight = $n.outerHeight();

showNotification();
setTimeout(hideNotification,8000);

function showNotification() {
$n.css('top',-nHeight).addClass('anim').css('top', 0);
}

function hideNotification() {
$n.css('top',-nHeight);
setTimeout(function() { $n.removeClass('anim'); }, 1000);
}
});
});
});

<?php
}


As you can see, I had embedded Javascript into PHP Function.



But problem occurred when I'm trying to upload an image.



Please referred to the image below.



enter image description here



The problem was solved once I removed the function above.



After that, I tried to inspect the error by using Firebug and it showed nothing.



enter image description here



enter image description here

0 comments:

Post a Comment