The following plugin is a trick to change ids="
by include="
each time a post is saved.
Please note the following:
-
You have to enable the option to search and change in all posts when the plugin is activated. I suggest a backup of the database if you use this option.
-
You have to adjust the get_cpt_sopt_27852()
function if you want to add other types of post (pages, custom posts, such as gallery or portfolio).
-
The search / replace is very basic. The ideal would be a RegEx ... The initial search is for [gallery
, if so and if the post has ids="
, this will be replaced with include="
.
<?php
/**
* Plugin Name: (SOPT) Ajustar Gallery shortcode para PostgreSQL
* Plugin URI: http://pt.stackoverflow.com/a/28228/201
* Description: Gambiarra para evitar erro no plugin de PostgreSQL.
* Author: brasofilo
* License: GPLv3
*/
# HABILITAR O SEGUINTE PARA ATUALIZAR TODOS OS POSTS NA ATIVAÇÃO DO PLUGIN
// register_activation_hook( __FILE__, 'ativar_sopt_27852' );
add_action( 'save_post', 'salvar_sopt_27852', 10, 2 );
/**
* Função auxiliar para definir os Post Types do plugin
*
# AJUSTAR array conforme necessário
*/
function get_cpt_sopt_27852()
{
return array( 'post', 'page', 'portfolio' );
}
/**
* Disparada na ativação do plugin
*
* Atualiza todos os posts/post-types já publicados
*/
function ativar_sopt_27852()
{
$args = array(
'post_type' => get_cpt_sopt_27852(),
'numberposts' => -1,
'post_status' => 'published'
);
$posts = get_posts( $args );
foreach ( $posts as $post )
{
if( FALSE !== strpos( $post->post_content, '[gallery' ) )
{
if( FALSE !== strpos( $post->post_content, 'ids="' ) )
{
$post->post_content = str_replace( 'ids="', 'include="', $post->post_content );
wp_update_post( $post );
}
}
}
}
/**
* Disparada a cada "Guardar" ou "Atualizar"
*/
function salvar_sopt_27852( $post_id, $post )
{
// Auto save?
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Correct post_type
if ( !in_array( $post->post_type, get_cpt_sopt_27852() ) )
return;
if( FALSE !== strpos( $post->post_content, '[gallery' ) )
{
if( FALSE !== strpos( $post->post_content, 'ids="' ) )
{
$post->post_content = str_replace( 'ids="', 'include="', $post->post_content );
# Evita loop do plugin
remove_action( 'save_post', 'salvar_sopt_27852' );
wp_update_post( $post );
add_action( 'save_post', 'salvar_sopt_27852', 10, 2 );
}
}
}