Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OrderAndCh4oS/7cabf77f9a2a61141dd73eda705ee0a5 to your computer and use it in GitHub Desktop.
Save OrderAndCh4oS/7cabf77f9a2a61141dd73eda705ee0a5 to your computer and use it in GitHub Desktop.
Polylang Bulk Action
<?php
global $wpdb;
/**
* and all the original post data then
*/
$post = get_post($post_id);
/**
* if you don't want current user to be the new post author,
* then change next couple of lines to this: $new_post_author = $post->post_author;
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/**
* if post data exists, create the post duplicate
*/
if (isset($post) && $post != null) {
/**
* new post data array
*/
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order,
'action' => 'translate_media',
);
/**
* insert the post by wp_insert_post() function
*/
$new_post_id = wp_insert_post($args);
/**
* get all current post terms ad set them to the new post draft
*/
$taxonomies = get_object_taxonomies(
$post->post_type
); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
/**
* duplicate all post meta just in two SQL queries
*/
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos) != 0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
if ($meta_key == '_wp_old_slug') {
continue;
}
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[] = "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query .= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
$languages = pll_languages_list();
$polylang = PLL();
$currentLanguage = $polylang->model->post->get_language($post_id); // Stores the old language
$translations = $polylang->model->post->get_translations($post_id);
foreach ($languages as $language) {
if ($language != $currentLanguage->slug) {
echo "nice";
$translations[$language] = $new_post_id;
pll_set_post_language($new_post_id, $language); // set new language
pll_save_post_translations($translations);
}
}
}
<?php
/**
* @package Polylang_Bulk_Action
* @version 1.0
*/
/*
Plugin Name: Polylang Bulk Action
Plugin URI: ...
Description: ...
Author: Sean Cooper
Version: 1.0
Author URI: http://orderandchaoscreative.com
*/
add_filter('bulk_actions-edit-post', 'register_my_create_language_posts');
function register_my_create_language_posts($bulk_actions)
{
$bulk_actions['create_language_posts'] = __('Create Language Posts', 'create_language_posts');
return $bulk_actions;
}
add_filter('handle_bulk_actions-edit-post', 'create_language_posts_handler', 10, 3);
function create_language_posts_handler($redirect_to, $doaction, $post_ids)
{
if ($doaction !== 'create_language_posts') {
return $redirect_to;
}
foreach ($post_ids as $post_id) {
// Perform action for each post.
include "duplicate-post.php";
}
$redirect_to = add_query_arg('bulk_language_posts', count($post_ids), $redirect_to);
return $redirect_to;
}
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );
function my_bulk_action_admin_notice() {
if ( ! empty( $_REQUEST['bulk_language_posts'] ) ) {
$count = intval( $_REQUEST['bulk_language_posts'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Created %s language post.',
'Created %s language posts.',
$count,
'create_language_posts'
) . '</div>', $count );
}
}
=== Polylang Bulk Action ===
Contributors: Sean Cooper
Requires at least: 3.0
Stable tag: 1.6
Tested up to: 4.6
Adds an item to bulk action that creates all language pages for Polylang.
== Description ==
Adds an item to bulk action that creates all language pages for Polylang.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment