Support » Plugin: SEO Facebook Comment » Performance Issue (fbAddComment )

  • Resolved greben99


    I am currently optimizing my code and your plugin is consuming at least 30% of the page load time, sometimes actually taking up to 7 seconds. I dont like relying on an external api to load the page. It all comes from the call to the facebook api restserver, for the page load. I notice there is a TODO on the function also, just wanted to warn you how bad the performance is…
    @todo improve performance by doing some pre-query so all the checks will be performed only when there is new comments

    Stacktrace below of whats taking over 800ms


Viewing 5 replies - 1 through 5 (of 5 total)
  • greben99


    I Fixed this problem by pulling out all the logic around syncing facebook comments with wordpress, and put that into its own function that runs on a schedule every 10 minutes, it goes through all the recent pages that have been viewed, and checks that the facebook comments are in sync. This has no impact on the users load time of a page 🙂

    Plugin Author bemcapaz


    Hi greben,

    This looks like a really good solution, do you mind sharing the code? I could add this for the next version of the plugin, saving some eletricity and power from thousands of servers running better optimized sites 😉

    I am having problem with comment box NOT loading every time (primarily Chrome and FireFox, but not so often IE as well).

    Then it started showing the box randomly with warning with the msg that domain cannot be reached.

    It used to work permanently until recently without any major changes on the site. Is this due to site pluginj/performance or is it FB updates that will eventually normalize?


    Hi Bemcapez,

    Sorry about the delay, been busy!

    Just digging through my code seeing what changes I made.

    The general vibe of it is I make the call to the AddFacebooktoDB function in the scheduled tasks, so delay it by say 5 minutes. (you could do it instantly, but I delay by 5 minutes just incase that page is getting hammered, only 1 event will be created every 5 minutes (could make it more)

    I initially I added this to the public function __construct()

    add_action( ‘SEOFacebook_AddComToDB’, array( &$this, ‘addFacebookCommentsToDB’ ), 10, 2);

    and then in
    public function fbAddComment($postUrl = ”, $postId = ”)

    I have added
    wp_schedule_single_event(time()+1800, “SEOFacebook_AddComToDB”, array($postUrl, $postId));

    which replaced the call directly to the function addFacebookCommentsToDB()

    I also added, which I think could be a good feature. Email notifications when a new facebook comment is added. As you don’t get notified by WordPress when you jam it into the database. So i added after the insert into the database. (maybe more info in the email would be good.

    $wpdb->insert($this->table, $facebookCommentData);

    $to = “”;
    $subject = “ALERT: New Facebook Comment Added -“.$postUrl;
    $body = $cleanComment;
    mail($to, $subject, $body);

    Hope that helps!!

    I forgot to add, I pulled out all the logic that adds the facebook comments and created the function –> public function addFacebookCommentsToDB($postUrl, $postId) {

    A different approach that I actually changed, because I had issues with caching and scheduled events.

    I added a meta value to the post, which then a scheduled task picks it up and performs the logic on it…

    for example

    in fbAddComments instead of calling schedule_single_event call this

    update_post_meta($postId, ‘seofb_checkcomments’, $postUrl);

    then somewhere else you add the following scheduled task that finds any post with that meta_key, does the add facebook stuff and then deletes the meta_key

    add_action(‘icb_seofb_addcomments’, array($this, ‘seofb_add_comments’));
    add_action(‘init’, array($this, ‘seofb_setup_schedule’));

    public function seofb_setup_schedule() {
    if (!wp_next_scheduled( ‘icb_seofb_add_comment_facebook’ ) && !defined(‘WP_INSTALLING’)) {
    wp_schedule_event(time(), ’10mins’, ‘icb_seofb_add_comment_facebook’);

    function seofb_add_comments() {
    $args = array(
    ‘post_type’ => ‘any’,
    ‘post_status’ => ‘publish’,
    ‘post_parent’ => ”,
    ‘posts_per_page’ => 9999,
    ‘meta_query’ => array(
    ‘key’ => ‘seofb_checkcomments’,
    ‘value’ => ”,
    ‘compare’ => ‘!=’

    $pages_to_check = get_posts($args);

    foreach($pages_to_check as $queue_page) {
    $option_value = get_post_meta($queue_page->ID, ‘seofb_checkcomments’, true);
    //Double check the email still hasn’t been sent!!
    if (!$option_value) {

    do_action(“SEOFacebook_AddComToDB”, $option_value, $queue_page->ID);

    delete_post_meta($queue_page->ID, ‘seofb_checkcomments’);

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Performance Issue (fbAddComment )’ is closed to new replies.