• Resolved dcole07

    (@dcole07)


    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?

Viewing 3 replies - 16 through 18 (of 18 total)
  • 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.

    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

    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 @ :

    https://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:

    https://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 :

    https://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

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘Using $wpdb outside wordpress’ is closed to new replies.