Change ids by include in shortcode gallery in Wordpress

1

I'm using Postgre and when creating image galleries the shortcode is automatically generated like this: [gallery ids="1,2,3,4,5"] . But this database does not accept 'ids', I should put 'include', thus: [gallery include="1,2,3,4,5"] . I do not know where I make this change in the code.

Wordpress Version 3.9.1

Using the PG4WP plugin to run Postgre

Obs. Using 'ids' returns this error:

  

WordPress database error: [ERROR: function field (bigint, integer,   integer, integer, integer) does not exist LINE 1: ... ND   ((wp_posts.post_status = 'inherit')) ORDER BY FIELD (wp _... ^ HINT: No   function matches the given name and argument types. You might need to   add explicit type casts.] SELECT wp_posts.ID FROM wp_posts WHERE 1 = 1   AND wp_posts.ID IN (56,55,54,53) AND (wp_posts.post_mime_type LIKE   'image /%') AND wp_posts.post_type = 'attachment' AND   ((wp_posts.post_status = 'inherit')) ORDER BY FIELD (wp_posts.ID,   56.55,54.53) LIMIT 0, 4

    
asked by anonymous 05.08.2014 / 15:49

1 answer

2

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 );
        }
    }
}
    
08.08.2014 / 01:29