当在 Wordpress 中单击发布按钮时,我想在 facebook 上分享一篇文章。此代码将不起作用。当我查看控制台时,它显示 Uncaught SyntaxError: Unexpected token <. 我该如何解决这个问题?
在我的主题functions.php中
add_action( 'admin_enqueue_scripts', 'wpse50770_add_admin_scripts', 10, 1 );
function wpse50770_add_admin_scripts( $hook ) {
global $post;
//Only need to enque script on the post.php page
//Optional: restirct by post type
if ( 'post.php' == $hook && 'post' == $post->post_type && isset($_GET['message']) ) {
$message_id = absint( $_GET['message'] );
wp_enqueue_script(
'wpse-notice',
get_template_directory_uri() . '/js/notice.js',
array('jquery')
);
$data = array( 'Message' => $message_id, 'title' => $post->post_title, 'post' => $post->post_content);
wp_localize_script( 'wpse-notice', 'wpsePost', $data );
}
}
在notice.js 中如下。
document.write("<div id='fb-root'></div>");
window.fbAsyncInit = function() {
FB.init({appId: 'myappid', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
jQuery(document).ready(function($) {
if( wpsePost.Message == 6 ){
FB.ui(
{
method: 'feed',
name: 'Company Name',
link: document.URL,
picture: '',
caption: wpsePost.title,
description: 'Click to see More',
message: wpsePost.post
});
alert(wpsePost.title+' Post published');
}else if( wpsePost.Message == 1 ){
alert('Post updated');
}
});