Site icon Hip-Hop Website Design and Development

How to use wordpress $polylang->model->set_post_language in custom php code?

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.