1

I am creating a custom content engine that posts artwork content (uploading images from dropbox, building the content from data in an Excel spreadsheet, using a custom table for content management etc.)

The issue which I have is that I want to post and link the EN and ES content as it is created and posted via wordpress.

Here you can see the code which is failing due to $polylang and models not available to my code.

// Insert the post into the database

    if($record->language == 'EN') {

        if ($post_id_en = wp_insert_post($my_post, true)) {

            // Insert spanish post
            $post_id_es = wp_insert_post($my_post, true);

            $polylang->model->set_post_language($post_id_en, 'en');
            $polylang->model->set_post_language($post_id_es, 'es');

            $polylang->model->save_translations('post', $post_id_en, array('es' => $post_id_es));

            set_meta_data($post_id_en,$record);
            set_meta_data($post_id_es,$record);
        }
    }
    elseif($record->language == 'ES') {

        if ($post_id_es = wp_insert_post($my_post, true)) {

            // Insert spanish post
            $post_id_en = wp_insert_post($my_post, true);

            $polylang->model->set_post_language($post_id_en, 'en');
            $polylang->model->set_post_language($post_id_es, 'es');

            $polylang->model->save_translations('post', $post_id_es, array('en' => $post_id_en));

            set_meta_data($post_id_es,$record);
            set_meta_data($post_id_en,$record);
        }
    }
    else{
        mylog("Inline: insert_post failed to find language.", "e");
    }

I am including Wordpress and Polylang on init:

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../../wp-load.php';

if ( ! defined( 'ABSPATH' ) ) {
    exit; // don't access directly
};

require_once __DIR__ . '/../../wp-admin/includes/upgrade.php';
require_once __DIR__ . '/../../wp-content/plugins/polylang/polylang.php';

Which has allowed me to use all Wordpress functions etc. However, Polylang seems to not want to play fair with me.

I have tried the usual tricks:

global $polylang;
$polylang = new Polylang();

After reading the documentation I even attempted to switch my approach to using the API functions "pll_*".

This approach found the function and WARNING!!! killed all languages and translation across the whole site.

I tried to include the api functions:

require_once __DIR__ . '/../../wp-content/plugins/polylang/include/api.php';
require_once __DIR__ . '/../../wp-content/plugins/polylang/polylang.php';

Then used:

if($record->language == 'EN') {

        if ($post_id_en = wp_insert_post($post, true)) {
            $posts = array(
                'es' => ''
            );

            // inserisco l'articolo nelle altre lingue: En Es Fr
            foreach ($posts as $language => $value) {
                $posts[$language] = wp_insert_post($post, true);
            }

            if (function_exists('pll_set_post_language') 
                && function_exists('pll_save_post_translations')) {

                pll_set_post_language($post_id_en, 'en');
                foreach ($posts as $language => $value) {
                    pll_set_post_language($value, $language);
                }

                pll_save_post_translations(array_merge(array($post_id_en), $posts));
            }
            $post_id_es = $posts['es'];
            set_meta_data($post_id_en,$record);
            set_meta_data($post_id_es,$record);
        }
    }
    elseif($record->language == 'ES') {

        if ($post_id_es = wp_insert_post($post, true)) {
            $posts = array(
                'en' => ''
            );

            // inserisco l'articolo nelle altre lingue: En Es Fr
            foreach ($posts as $language => $value) {
                $posts[$language] = wp_insert_post($post, true);
            }

            if (function_exists('pll_set_post_language') 
                && function_exists('pll_save_post_translations')) {

                pll_set_post_language($post_id_es, 'es');
                foreach ($posts as $language => $value) {
                    pll_set_post_language($value, $language);
                }

                pll_save_post_translations(array_merge(array($post_id_es), $posts));
            }
            $post_id_en = $posts['en'];
            set_meta_data($post_id_en,$record);
            set_meta_data($post_id_es,$record);
        }
    }
    else{
        mylog("Inline: insert_post failed to find language.", "e");
    }

Perhaps it was expecting a default language to be set or some other configurations prior to calling the functions.

0

Turns out that the language relationship is simple and set in just a few functions:

// Insert the EN post which returns the EN post id.

$post_data_en = array( 
// add EN post data like the title and content
)
$post_id_en = wp_insert_post($post_data_en, true);

// Then set the language of the post

pll_set_post_language($post_id_en, 'en');

// Insert the ES post which returns the ES post id.

$post_data_en = array( 
// add ES post data like the title and content
)
$post_id_es = wp_insert_post($post_data_es, true);

// Then set the language of the post

pll_set_post_language($post_id_en, 'es');

// Then bind them together with a translation relationship

pll_save_post_translations(array('en' => $post_id_en, 'es' => $post_id_es));

The posts are now linked as translations of each other.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.