Keep Settings when Changing URL - Wordpress - Theme: Adventure - Organic Theme

Asked

Viewed 165 times

2

I’m having trouble in the theme settings by changing the URL of a wordpress site using the Organic Theme’s Adventure theme.

I made some scripts to change the functions in the database, which is ok! And works with other themes perfectly.

However, Organic Themes' Adventure theme uses a single table column wp_options to store the settings.

Every time I change the url (with different size) the theme settings are lost.

I noticed that they work with a strange pattern (for my limited knowledge), storing the theme settings in a single column, using the link size to generate the settings.

Follows as arrow in the database, in the column option_value:

s:12:"header_image";s:62:"http://www.yoursite.com.br/wp-content/uploads/2015/06/logo.jpg";

Note that the number after the s: represents the size of the string in front: s:62: is the size of the string with the image logo.jpg.

Changing the url to http://newsite.com.br I have to adjust not only the url but the size indicated in s:XX.

Does anyone know any way to do this update? Via plugin or code?

It takes a lot of time to redo the settings of the theme and in case of restoration of the site, this will be disfigured for a few minutes until the configuration of the theme is all redone manually (again).

Grateful!

2 answers

1

To modify Urls in the database you must always take into account the change without errors of serialized values, as in your example:

s:12:"header_image";s:62:"http://www.yoursite.com.br/wp-content/uploads/2015/06/logo.jpg";

Normally, I use the tool Wordpress Serialized PHP Search Replace Tool when the need is simple.

If you are automating the process, then you will need PHP functions serialize() and unserialize().

  • Thanks for the answer... really, any and all plugin or migration system I could test couldn’t meet me contentedly. I developed a very "simple" bash script to resolve the issue and solved the problem. Should I post my script here? Abs

1


I developed a script in bash to solve the problem.

Below is the full script:

#!/bin/bash

# 1. Dados Gerais

# 1.1 URL
URL_OLD=http://very_old_url.com.br
URL_NEW=http://novo_url.com.br

# 1.2 Database
USER=db_user
PASS=db_pass
DATABASE=db_name
WP_TABLE=un_options
WP_FIELD=option_value
THEME=theme_mods_organic_adventure-child

# 2. Configurações iniciais

# 2.1 Diferença de tamanho das URL´s
VAL=$(( ${#URL_OLD} - ${#URL_NEW} ))

# 2.2 Valor atual no banco de dados
RESULT=$(mysql -u $USER -p$PASS $DATABASE -s -N -e 'SELECT '$WP_FIELD' FROM '$WP_TABLE' WHERE option_name="'$THEME'";')

# 2.3 Matrix com todos os índices que possuem ocorrência de 'http://' no banco de dados
if [[ $RESULT == *"http://"* ]]; then
    MUDAR=($(echo $RESULT | grep -b -o http | awk 'BEGIN {FS=":"}{print $1}'))
fi

# 2.4 Loop para mudar o tamanho no banco de dados
for i in "${MUDAR[@]}"
do
    # 2.4.1 Busca o campo atual
    STR=${RESULT:$i-8:14}

    # 2.4.2 Busca o tamanho atual
    TAM=$( echo $STR | egrep -o "[0-9]*" )

    # 2.4.3 Roda a função se o valor for menor que 3... se for maior tem de ajustar essa parte @TODO
    if [[ ${#TAM} < 3 ]]; then
        nTAM=$(($TAM-$VAL))             # New lenght
        nSTR=${STR/$TAM/$nTAM}          # Fix the Current Number
        RESULT=${RESULT/$STR/$nSTR}     # Update the string
    fi
done

# 3. Grava no banco de dados

# 3.1 Prepara a string com barra antes das aspas duplas para gravar no mysql
RESULT=$( echo $RESULT | sed -e 's/\"/\\"/g' )

# 3.2 Atualiza o banco de dados com as novas configurações
mysql -u $USER -p$PASS $DATABASE -s -N -e 'UPDATE '$WP_TABLE' SET '$WP_FIELD'="'${RESULT}'" WHERE option_name="'$THEME'";'

return

I hope it will be useful to someone as well. To use scirpt just change the settings in the '1 field'.

Thank you!

Browser other questions tagged

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