• Resolved Jason

    (@itzjonas)


    I’ve gotten the comment_form() function to work great for single install wordpresses, but I can’t figure out how to get a comment form from any blog in a multisite. Here’s what I’m doing so far:

    Created a page called wp-get-comment-form.php in the root of WP install that will look for a Blog ID # and Post ID # (e.g. wp-get-comment-form.php?p=45b=9)

    <?php
    
    // Need to be in WP's Loop but without theme
    define('WP_USE_THEMES', false);
    require('wp-blog-header.php');
    
    $postid = $_GET['p'];
    $blogid = $_GET['b'];
    
    // Tell me where I'm at
    switch_to_blog($blogid); // deprecated and doesn't really switch
    
    // Check to see if the post exists
    if ( is_single( $postid )) {
    	echo get_the_title($postid); // Title of the post we are commenting on
    	comment_form( $arg , $postid );
    }
    else
    	echo "<br/><br/>Post doesn't exist!";
    
    restore_current_blog();
    
    ?>

    However, the above code just returns the default or root WordPress posts. Since the actual locations of the blogs are all logically-based I can’t just put this file somewhere else to invoke the correct location. Any help would greatly be appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    What are you doing, that you can’t call the comment form in the page like normal?

    switch_to_blog($blogid) may be failing because it’s outside of WP and, as such, doesn’t know what blog it’s on.

    Thread Starter Jason

    (@itzjonas)

    Hi Ipstenu! So we meet again ?? I can’t call the comment form from the page like normal because I have a mobile application that reads in a RSS feed. I want the users to be able to comment if they’d like to so I would need to load just the comment form when they want it. Since I’m not actually at the page with the information, I need to send a request to get that information. I figured the easiest way would be to do it this way and I have gotten it to work great in a single installation environment.

    The switch_to_blog() function seems to be working because when I add these lines after the call I get the correct blog name:

    $current_blog_details = get_blog_details( array( 'blog_id' => $blog_id ) );
    echo $current_blog_details->blogname;

    However, once I try to do:

    echo get_the_title($postid); // Title of the post we are commenting on
    comment_form( $arg , $postid );

    I get the default blog, instead of the switched to blog.

    I’ve read that requiring wp-blog-header.php will put you inside WP so this would explain why switch_to_blog is workin as far as doing array calls. (FYI, I defined wp_use_themes as false because then I have just a straight up plain page with no styling)

    I hope this information helps.

    Thread Starter Jason

    (@itzjonas)

    Alright, after talking this through with someone else, this actually works fine as is! Here’s how to do it:

    1. I simplified the code without the blog id and switching. Here’s what it looks like:

    <?php
    
    // Need to be in WP's Loop but without theme
    define('WP_USE_THEMES', false);
    require('wp-blog-header.php');
    
    $postid = $_GET['p'];
    
    // Check to see if the post exists
    if ( is_single( $postid )) {
    	echo get_the_title($postid); // Title of the post we are commenting on
    	comment_form( $arg , $postid );
    }
    else
    	echo "Post doesn't exist!";
    
    ?>

    2. I then access the file using the URL separation (sub-domain or sub-directory) instead of the base location. So for me it’s by sub-directory and looks like this: https://mydomain.com/us/wp-get-comment-form.php?p=34 Before I was trying to access it by https://mydomain.com/wp-get-comment-form.php?p=34&b=9 – I forgot that the logical separations still treats what’s physical files are at the base location as if it’s in sub-directory or sub-domain location!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Just the Comment Form in Multisite for Any Blog’ is closed to new replies.