WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using $wpdb outside wordpress (19 posts)

  1. dcole07
    Member
    Posted 5 years ago #

    I'm writing a plugin, but a AJAX request is calling a php file and I want to create tables and save data that have the same prefix as the other WordPress tables.

    The problem is you can NOT just global $wpdb and have it work, because no other files are involved! What file should I include so that the $wpdb could be global?

  2. scribu
    Member
    Posted 5 years ago #

    Probably wp-includes/wp-db.php.

  3. whooami
    Member
    Posted 5 years ago #

    whenever you want to use wordpress fucntions OR variables outside of wordpress, you need to include wp-blog-header.php

  4. jeremyduffy
    Member
    Posted 5 years ago #

    How do you include it? I know it's in the root, but how do I reference it? I tried many things, but it either didn't work or gave me an access denied error.

    All I want is to be able to reference the WPDB value in my AJAX handler file.

  5. whooami
    Member
    Posted 5 years ago #

    How do you include it?

    http://us.php.net/include/

    there are hundreds, if not thousands, of examples of php includes inside all of the wordpress files themselves.

    Actually, thinking about that again the devs tend to use requires but the format is identical. :)

  6. jeremyduffy
    Member
    Posted 5 years ago #

    To be specific, I have a template that calls an AJAX function and loads another file (which isn't a template, just a PHP file). How can I have WordPress functions and variables available in that file?

  7. jeremyduffy
    Member
    Posted 5 years ago #

    Whooami, I used include, but it gave me an error. Also, how do I reference it? I can just use "/wp-blog-header.php" for the one in root, but what about when the wordpress install isn't in the root? How do I find it or does it just have to be hard-coded?

  8. whooami
    Member
    Posted 5 years ago #

    did you look at the link I provided?

    You reference it, using the path to it, from the file you are including it in.

    do we need a primer on paths?

    ./ == current directory
    ../ == one directory up
    ../stuff == one directory up and then into stuff.

    ../../ == 2 directories up.

    ../../morestuff == 2 directories up and then into morestuff

  9. jeremyduffy
    Member
    Posted 5 years ago #

    Error:
    Warning: require(./wp-blog-header.php) [function.require]: failed to open stream: No such file or directory

  10. whooami
    Member
    Posted 5 years ago #

    ./things == inside the current directory and then into the subdirectory named things.

    --

    If you stop pasting, and typing and start reading ..

  11. jeremyduffy
    Member
    Posted 5 years ago #

    Hmm... I suppose the themes are always the same depth... I just don't want to make it difficult for people who use my code and have non-standard installations. So I suppose I'll have to use require("../../../wp-blog-header.php);

    (using that, it's working. As long as no one is putting their theme and deeper or more shallow than normal, I guess it will work).

    Thanks!

  12. whooami
    Member
    Posted 5 years ago #

    if you are doing this for a plugin, there are other options. there are all kinds of preassigned variables that could handle simplifying that.

  13. jeremyduffy
    Member
    Posted 5 years ago #

    Not a plugin in this case. Actual file here:

    http://www.jeremyduffy.com/advanced-blog-search/

  14. Glenn Ansley
    Member
    Posted 5 years ago #

    Does anyone know how I would go about doing this now that the wp-content directory can be moved if the blog isn't in the site root?

  15. Glenn Ansley
    Member
    Posted 5 years ago #

    Nevermind: I wound up just passing the ABSPATH constant through the ajax request as a var. Then I included the file I needed.

    mysack.setVar( "wp_root" , "<?php echo ABSPATH;?>" );

  16. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    ... Ugh.

    If you have to have an external file include the WP functions because you're referencing them directly like that, then you're pretty much doing it wrong.

    Consider alternate approaches, like passing the actual data you need to the files directly, or generating the files on the fly from the plugin, or some such thing.

    Because every time you include the entirety of WordPress again when only one page is being viewed, you increase the server load by double. And God kills a kitten.

  17. Glenn Ansley
    Member
    Posted 5 years ago #

    heh...
    My problem is that I have a standalone script in my plugin folder that is not being included by WP when it's called. I need to 1) update_usermeta(), and then 2) return get_usermeta().

    like passing the actual data you need to the files directly

    I don't think I can do this because the script updates the DB and then I need the new info sent back.

    generating the files on the fly from the plugin

    Could you expound on this? Do you mean create my own connection to the DB and update / select w/o $wpdb?

    Thanks for the feedback.

  18. superbibi75
    Member
    Posted 5 years ago #

    Hi,

    I walk arround the same problem, so I check WP code and extract the needed code to get the HTML of the needed page_id, post_id, posts-in-month, outside wordpress environement...

    In my case, I wanted to use wordpress as a backoffice.
    I pasted wordpress pages content in different part of a simple website, and then use wordpress to manage it (and the blog too !).

    All you need to do is to past within your own library this piece of code ((where "blog" is my root path for wordpress))

    <?php
    include_once($_SERVER['DOCUMENT_ROOT'].'/blog/wp-load.php' );
    function wp_get_page_content( $type='page_id', $id=0 )
    {
    	$_GET[$type] = (int)$id;
    	$wp_did_header = true;
    	wp();
    	if ( have_posts() )
    	{
    		while ( have_posts() )
    		{
    			the_post();
    			$content = get_the_content();
    			$content = apply_filters('the_content', $content);
    			$content = str_replace(']]>', ']]>', $content);
    		}
    	}
    	return ( $content!=NULL )?$content:'';
    }
    ?>
    • Then whenever you request a wordpress page content outside wordpress framework.
      For instance to retrieve wordpress home html content, past that into your page :
    <div><?=wp_get_page_content();?></div>
    • To get wordpress posts contents of a specific month (in HTML) do :
    <div><?=wp_get_page_content('m',200811);?></div>
    • to get wordpress page content of a specific page_id (in HTML) do :
    <div><?=wp_get_page_content('page_id',4);?></div>

    to get wordpress post content of a specific post ID (in HTML) do :

    <div><?=wp_get_page_content('p',1);?></div>

    And so one with 'year', 'day' ...
    now you have the basis, build your how library with other stufs and feed me back !

    Brice Pissard

  19. superbibi75
    Member
    Posted 5 years ago #

    I'll also give you another pretty good code to convert your wordpress page content in PDF.

    I'm sure that some of you are interested on it !

    so let's take the previous code (in my previous post).

    At the very top of your document past that :
    (at the very top because FPDF, modify "Header();", and they can not be even a "space" caracter before beginig the PDF document...)

    <?php
    ob_start();
    require_once($_SERVER['DOCUMENT_ROOT'].'/includes/libraries.wordpress.php');
    $content = wp_get_page_content('page_id',6);;
    
    if ( strtolower($_GET['mode'])=='pdf' )
    {
    	$content = utf8_decode($content);
    
    	require_once($_SERVER['DOCUMENT_ROOT'].'/includes/libraries.class.HTML2PDF.php');
    	$pdf = new HTML2PDF('P','A4','US');
    	$pdf->WriteHTML($content,'');
    	$pdf->Output();
    }
    else
    {
    ?>
    <html>
       <body>...
    	<div><?=$content;?></div>
       ...</body>
    </html>
    <?php
    }
    ?>

    In the library file :

    require_once($_SERVER['DOCUMENT_ROOT'].'/includes/libraries.wordpress.php');

    you have to past the previous function wp_get_page_content()

    <?php
    include_once($_SERVER['DOCUMENT_ROOT'].'/blog/wp-load.php' );
    function wp_get_page_content( $type='page_id', $id=0 )
    {
    	$_GET[$type] = (int)$id;
    	$wp_did_header = true;
    	wp();
    	if ( have_posts() )
    	{
    		while ( have_posts() )
    		{
    			the_post();
    			$content = get_the_content();
    			$content = apply_filters('the_content', $content);
    			$content = str_replace(']]>', ']]>', $content);
    		}
    	}
    	return ( $content!=NULL )?$content:'';
    }
    ?>

    You need the FPDF library and it's Class extends HTML2PDF freely dowloadable @ :

    http://html2pdf.spipu.net/telechargement.php

    Now when you call your page in your favorite browser (and I'm prety sure it's firefox), add the GET parameter mode='PDF'

    Test the result in real:

    http://www.reversoform.com/cgv/

    give you the normal customize page with wordpress content, (but you can not know it, as it's not a blog)

    and :

    http://www.reversoform.com/cgv/?mode=pdf

    give you a PDF parsed "on-the-fly" with wordpress content (backward editable, with picture and all...).

    Hope you'll enjoy it !

    Brice Pissard

Topic Closed

This topic has been closed to new replies.

About this Topic