Trade ids by include in gallery shortcode in Wordpress

Asked

Viewed 159 times

1

I am using Postgre and when creating image galleries the shortcode is generated automatically like this: [gallery ids="1,2,3,4,5"]. But this database does not accept 'ids', I must put 'include', so: [gallery include="1,2,3,4,5"]. I don’t know where I make that change in the code.

Wordpress Version 3.9.1

Using the PG4WP plugin to run Postgre

Note. 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

  • What version of WP?

  • Wordpress Version 3.9.1

  • I don’t think that’s the problem, because at the end of the day, ids is converted into include in shortcode processing. Or if you use [gallery include="1,2,3,4,5"] works? What solution are you using to run Posgresql? Have you read this?

  • PG4WP plugin to run Postgre

  • There are some tips to fix the plugin on Not Working with 3.9... I asked two other questions in my previous comment...

  • When I use [gallery include="1,2,3,4,5"] it works. This plugin repair tip I had already made.

Show 1 more comment

1 answer

2


The following plugin is a gambiarra to exchange ids=" for include=" each time a post is saved.

Take into account the following:

  • You have to enable the search and switch option in all posts when the plugin is enabled. I suggest a database backup if you use this option.

  • You have to adjust the function get_cpt_sopt_27852() if you want to add other post types (pages, custom posts, like gallery or portfolio).

  • The search/replace is very basic. The ideal would be a Regex... The initial search is by [gallery, if yes and if the post has ids=", this will be replaced by include=".

<?php
/**
 * Plugin Name: (SOPT) Ajustar Gallery shortcode para PostgreSQL 
 * Plugin URI:  /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 );
        }
    }
}
  • That’s right! Thank you! Very nice to find people available to help us! Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.