The MU forums have moved to WordPress.org

Get blog_id in external files (15 posts)

  1. osynligstefan
    Member
    Posted 5 years ago #

    Hello!

    Is it possible to get the current blog id in external plugin files? I've solved it by always sending the blog_id in urls, but thats not optimal.

    Examples of where i need the blog id:
    - External ajax calls
    - Generated xml-files
    - Tinymce plugins (popup)
    ...

    Thanks

  2. osynligstefan
    Member
    Posted 5 years ago #

    To clarify: The problem is, when including wp-functionality in an external file, wordpress doesn't know which blog im on so it defaults to the first one.

  3. tdjcbe
    Member
    Posted 5 years ago #

    Do a file include with a call to wp-blog-header.php and then I think it's a function call get_blogid_from_url with the passing of the blog URL that you need. I forget the specific function call and can't check from here but it's in the wpmu-functions.php file.

    Do a google for including wordpress functions externally. Has to be tons of examples on this.

  4. tdjcbe
    Member
    Posted 5 years ago #

    get_id_from_blogname()

  5. osynligstefan
    Member
    Posted 5 years ago #

    Great function, thanks! What would be the easiest way to pass blog name to external files? GET?

  6. tdjcbe
    Member
    Posted 5 years ago #

    It should just be a function call. If you do that include, it should be accessable.

  7. osynligstefan
    Member
    Posted 5 years ago #

    How can I then use the plugin in more than one blog?

    If i use get_id_from_blogname('blog1') it's static. I need to get the id dynamicly depending on which blog your visiting.

    I'm terrible at explaining the problem, sorry. Just ask anything you need to know in order to help me.

    Thanks.

  8. tdjcbe
    Member
    Posted 5 years ago #

    That would depends on the software you;re using to run the other site.

  9. osynligstefan
    Member
    Posted 5 years ago #

    Software? I'm not sure I understand. My problem is to create a wordpress plugin that works in a wp-mu environment (more than one blog in the same mu-setup) and which has external files such as tinymce-windows, ajax calls and so on.

  10. astrader
    Member
    Posted 5 years ago #

    I'm using 2.7 and I find I can just do the following:

    global $current_blog;
    $blog_id = $current_blog->blog_id;

    And $blog_id contains the right value.

  11. raidelp
    Member
    Posted 5 years ago #

    I'm looking for something similar. As osynligstefan, im working with external files in the development of my first mu plugin. :)
    my question:
    Is there a way to set the current_blog for an external file, so the wp functions using global variables work as i expect for the blogi want?
    for instance: my ajax plugin calls an external file that in some place call this wpfunction : wp_set_post_categories( $postid, $post_category ) ; as the only possible parameters of this function are $postid and $post_category and as in the external files the global variable $current_blog is set to the first blog, [blog_id]=1, when calling this function will affect the post in the [blog_id]=1, and off course not the post in the blog from where i made the ajax call.

    something like this would be amazing: set_current_blog($blogid); (something similar to set_curent_user)or adding an extra parameter to most of functions wp_set_post_categories( $postid, $post_category, $blogid ) ;
    i did my search and research and played around with this function with no luck :(
    switch_to_blog('$new_blogid');
    any ideas out there? I need help.
    thanks

    note:sorry for my possible english language mistakes.

  12. SteveAtty
    Member
    Posted 5 years ago #

    What do you mean by "external" files?

    Do you mean files completely outside of the WPMU directory structure?

    For getting stuff from WPMU into Javascript I simply have a file that contains code like this:

    echo '<script type="text/javascript"> var linktype=1; cplogid='.$wpdb->blogid.'</script>';

    which means I can access WPMU variables from inside javascript.

  13. raidelp
    Member
    Posted 5 years ago #

    hey SteveAtty! thanks for a fast response.
    well I'm not sure if I'm correct technically speaking. when i say "external" files i mean a php script called by my java script in the running blog. the php script called is inside the WPMU directory structure but as far as i know is not loaded by default, i placed it in a folder inside the mu-plugins folder together witha couple of images and myajax.js script (..\mu-plugins\myexternalfiles\externalfile.php) and my main pluging script is in the base of the folder mu-plugins (..\mu-plugins\myplugin.php). the plugin is loaded correctly, java stuffs work fine but if for instance i var_dump($current_blog); it will show that is set with the first blog of my site [blog_id]=1, regardless the blog i call the script, then if i use a function like: wp_set_post_categories( $postid, $post_category ) ; it will affect the post in the first blog.
    I'm passing the blog_id i need from the pluging script to the "external" script by the url as GET request.
    i just tried this:
    `unset($current_blog);
    $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %s", $blogid.'/') );`
    and it works, $current_blog gets the right values even though wp_set_post_categories( $postid, $post_category ) ; keeps working with the first blog.
    Am I asking a crazy thing? maybe im in a wrong path :|

  14. raidelp
    Member
    Posted 5 years ago #

    I realized that also $wpdb needs to be reset with new values* so i came out with my own set_current_blog($blogid) function for my plugin... so far it works fine but it might have some bugs though.

    i have developed some practical plugins for wp to solve my own needs in the past but I'm new with wp um.
    maybe someone have a more elegant solution for this?

  15. osynligstefan
    Member
    Posted 4 years ago #

    I almost solved it like you, raidelp.

    Plugin that sets the blogid in javascript:

    add_action('wp_head', 'im_head' );
    
    function im_head() {
    	global $wpdb;
    	?>
    	<script type="text/javascript">
    		var blogid = '<?php echo $wpdb->blogid; ?>';
    	</script>
    	<?php
    }

    ...and in ajax.php:

    <?php
    require_once('../../../wp-config.php');
    
    if(!isset($_POST['blogid']) || !is_numeric($_POST['blogid']) || $_POST['blogid'] <= 0)
    	die('Blog id not set.');
    
    switch_to_blog($_POST['blogid']);
    
    ...
    ?>

    Doesn't feel like a good solution though.

About this Topic

  • Started 5 years ago by osynligstefan
  • Latest reply from osynligstefan