WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Dynamically Generating Content Without Using Publish Features (3 posts)

  1. Fedge
    Member
    Posted 1 year ago #

    Hello :) I'm working on my second plugin and this one needs to display dynamic content rather than working from within the publishing system.

    I've got my files in the plugin's folder and I'll be linking users directly to them as users who are not specifically designated as allowed should not see these pages.

    I did this:

    define('WP_USE_THEMES', true);
    
    require('../../../wp-blog-header.php');

    and it just outputs the homepage of my WordPress installation. Can I somehow override the content it's displaying? I've been searching for two days on Google and it just keeps showing me more articles about inserting pages or posts into the database and I definitely do not want to use that approach.

  2. toocoolone
    Member
    Posted 1 year ago #

    I've encountered this problem before while trying to get AJAX data out of WordPress and developed two ways of handling this. The key is to call your WordPress site in a valid way. There are two ways to do it and both require some knowledge of PHP. The first is to make a special "AJAX" template using a child theme and a special template.php file that does not provide the header and footer of your site. This way, the bare article from your DB is served up. It isn't very dynamic, but it will get the job done in a pinch.

    The other way is to use a plugin to "override" WP's delivery of content while allowing things like DB access to remain. You serve your own content in a special way using a plugin. (Sounds like what you're trying to do.) There's a basic plugin that does this. It's not a plugin in the truest form of the word -- you have to edit the PHP files to get the content you want. It does allow you to override WP and deliver customized content without a header and footer. The method it uses to do this is what you're interested in because it overrides WP's default content engine and outputs its own stuff. You can read about it here and download an archive here.

    The key part I think you're interested in, however, is this:

    $uri = null; $qry = array();
    if( strpos( $_SERVER['REQUEST_URI'], '?' ) !== false ) {
    	list($uri, $qry) = explode( '?', $_SERVER['REQUEST_URI'] );
    	$qry = $_GET;
    } else {
    	$uri = $_SERVER['REQUEST_URI'];
    }
    
    $uri = rtrim( $uri, '/' );
    $uri = ltrim( $uri, '/' );
    $tokens = explode( '/', $uri );
    if( array_shift( $tokens ) == $this->sniff ) {
    	$this->controller = ucwords( strtolower( array_shift( $tokens ) ) );
    	$this->action = ucwords( strtolower( array_shift( $tokens ) ) );
    	foreach( $tokens as $token ) {
    		$this->tokens[] = $token;
    	}
    	if( is_array( $qry ) ) {
    		foreach( $qry as $key => $val ) {
    			$this->tokens[$key] = $val;
    		}
    	}
    	if( $this->controller == '' ) {
    		$this->controller = $this->default_controller;
    	}
    	$this->template = DWP_PLUGIN_DIR . '/template.php';
    }
    add_action( 'ajaxifier_do_framework', array( &$this, 'get_view' ) );
    add_filter( 'template_include', array( &$this, 'get_template' ) );

    This is the __construct() method of a larger object. But what's happening here is $this->sniff = 'ajax' for instance. Then when a user visits http://yoursite.com/ajax/index/view this code will call another abstract object View::IndexView() and pass any arguments along to it.

    It uses a custom method to query the database and deliver the content, when called, or allows WordPress to continue unabated if not. The "template_include" hook allows us to use a custom template to deliver the content without a header and footer.

    I think the concept of what you are trying to do is in this. You can serve content that you create yourself, from a different database or by any method you choose this way while still allowing WordPress to handle user authentication and database access. You could download the whole package and have a look at it. It may help you in your plugin development.

  3. Fedge
    Member
    Posted 1 year ago #

    I figured it out.

    require_once('../../../wp-load.php');
    get_header();
    
    //output your content here
    
    get_footer();

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.