Paste last WordPress post in CakePHP

1

When loading:

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

I'm having the following error:

  

Fatal error: Can not redeclare __()

How can I load the last blog post into a project in CakePHP?

    
asked by anonymous 14.08.2014 / 03:59

3 answers

2

I do not advise you to try to load all of WordPress inside the project in Cake, it will call a lot of functions you do not need, it's a lot to just access a post.

Finally, it is possible to do a direct query in the WordPress database or soon 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);
}
    
14.08.2014 / 04:34
1

Actually, you do not have the need to load all this, I did it this 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);
    
14.08.2014 / 06:24
0

You can also use XML-RPC for check out WordPress . Using a simple PHP file, we issue a cURL request to the http://example.com/wordpress-folder/xml-rpc.php address by calling some specific method. This returns an XML file with the query results.

<?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 );

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

/**
 * 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 (I suggest doing 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;
}
    
14.08.2014 / 17:10