Pick up last Wordpress post on Cakephp

Asked

Viewed 405 times

1

When loading:

define('WP_USE_THEMES', false);
require(ROOT.'\blog\wp-blog-header.php');

I’m having the following mistake:

Fatal error: Cannot redeclare __()

How can I upload the latest blog post to a project in Cakephp?

  • You cannot use the function require more than once for the same file.

3 answers

2


I do not advise you to try to load all Wordpress within the project in Cake, will draw a lot of functions that you do not need, is a lot to just be able to access a post.

Anyway, it is possible to make a direct query in the Wordpress database or just use the feed in this way:

$feed_url = 'http://localhost/blog/feed/';
$content = file_get_contents($feed_url);
$feed = new SimpleXmlElement($content);

if (isset($feed->channel->item[0])) {
    $entry = $feed->channel->item[0];

    printf('<a href="%1$s" title="%2$s">%2$s</a>', $entry->link, $entry->title);
}

1

Really, there’s no need to carry all this, I did it that way:

$post = $this->Post->query(
        "SELECT
            posts.id,
            posts.post_title AS title,
            posts.post_date,
            posts.guid,
            files.meta_value AS filepath
        FROM
            wp_posts posts
        INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
        INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
        WHERE files.meta_key = '_wp_attached_file' ORDER BY posts.post_date DESC LIMIT 1"
    );
    $this->set('post', $post);

0

One can also use XML-RPC for consult Wordpress. Using a simple PHP file, we trigger a Curl request to the address http://example.com/wordpress-folder/xml-rpc.php calling some specific method. This returns an XML file with the results of query.

<?php
/**
 * Executa chamada de XML-RPC
 */
function query_last_posts( $methodName, $url, $parameters )
{
    $request = xmlrpc_encode_request( $methodName, $parameters );
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $request );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 1 );
    $results = curl_exec( $ch );
    curl_close( $ch );
    return $results;
}

$blog_id = '1';
$user = 'username';
$pass = 'password';
$post_type = 'post';
$number_posts = '2'; 
$args = array( $blog_id, $user, $pass, $post_type, $number_posts );
$response = query_last_posts( 'b5f.getRecents', 'http://plugins.dev/xmlrpc.php', $args );

As I did not have good results using the standard methods, I created a custom method (b5f.getRecent). But before explaining this customization, here are some auxiliary functions to see the return of XML-RPC:

/**
 * Função auxiliar: converte XML em JSON
 */
function print_json( $string )
{
    # http://stackoverflow.com/a/20431742/1287812
    $xml = simplexml_load_string( $string );
    $json = json_encode( $xml );
    $array = json_decode( $json, TRUE );
    printf( '<pre><code>%s</code></pre>', print_r( $array, true ) );
}
/**
 * Função auxiliar: dá saída a XML no browser
 */
function print_xml( $string )
{
    header("Content-type: text/xml");
    echo $string;
}
/**
 * Função auxiliar: imprime o node principal do valor retornado
 */
function print_xml_node( $string )
{
    $xml = simplexml_load_string( $string );
    printf( '<pre><code>%s</code></pre>', print_r( $xml->params->param->value->array->data->value, true ) );
}

# print_json( $response );
# print_xml( $response );
print_xml_node( $response );

To create custom methods, just create a plugin and use the hook xmlrpc_methods (suggest making a Must Use plugin):

<?php
/**
 * Plugin Name: Chamada personalizada ao XML-RPC
 * Description: Novos métodos para o http://example.com/xml-rpc.php do WordPress
 * Plugin URI: 
 * Author: brasofilo 
*/
add_filter( 'xmlrpc_methods', 'xmlrpc_methods_sopt_28962' );

function xmlrpc_methods_sopt_28962( $methods ) 
{
    $methods['b5f.getRecents'] = 'b5f_get_recent';
    return $methods;
}

function b5f_get_recent( $args ) 
{
        global $wp_xmlrpc_server;
        $wp_xmlrpc_server->escape( $args );

        $blog_ID     = (int) $args[0];
        $username  = $args[1];
        $password   = $args[2];
        $post_type  = $args[3];
        $numberposts = $args[4];
        $extra = $args[5];

        if ( !$user = $wp_xmlrpc_server->login( $username, $password ) ) 
            return $wp_xmlrpc_server->error;

        $category_int = (int) $category;    
        $post_args = array(
                'numberposts' => $numberposts,
                'posts_per_page' => $numberposts,
                'post_type' => $post_type,
                'post_status' => 'publish'
            );

        if( is_array( $extra ) )
            $post_args = array_merge( $post_args, $extra );

        $posts_list = query_posts( $post_args );   
        if ( !$posts_list ) {
            $wp_xmlrpc_server->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
            return $wp_xmlrpc_server->error;
        }
        return $posts_list;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.