Share the post on facebook when publish the new post in WordPress

I want to share a post on facebook when the publish button is clicked in WordPress. This code won’t work. When I look at the console, it shows that Uncaught SyntaxError: Unexpected token <. How can I resolve this problem?

In my theme functions.php

Read More
    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 );
    }
}

In notice.js as follows.

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');
    }

});

Related posts

Leave a Reply