Wordpress option_tree overwriting - php

I'm using Wordpress 4.6.1 with the theme Goodlife.
Whenever I change the domain, my theme loses all settings (theme options).
Analyzing database, I realized that he loses the values changed in "option_tree" option.
To change domain, I use these queries:
UPDATE wp_posts SET guid = REPLACE(guid,'old_url','new_url');
UPDATE wp_posts SET post_content = REPLACE(post_content,'old_url','new_url');
UPDATE wp_options SET option_value = REPLACE(option_value,'old_url','new_url');
I made a simple test to confirm, and copied the value of this option and enter directly into the database. Just that I load the wordpress that the value is lost again.
Now I have the "option_tree" value in a txt file, but the wrong value in the database, how can I use the real value in my website without it being overwritten?

Without seeing exactly how the data is structured, I'd bet 100% that the theme option corruption is due to the search and replace screwing up serialized data. Since serialized data defines the exact length of it's data, a search and replace (unless the before and after terms are exactly the same) can have the effect of corrupting all the data in the field. Most theme options are saved as serialized data.
Instead of trying to run your custom commands, I've found a LOT of success with this search and replace script meant for this exact purpose:
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
Follow the install instructions and run the search and replace through their tool. It correctly handles serialized data and prevents errors from occurring in the data.

Related

Value dissapears on live site after editing DB value

I have an issue with a Wordpress premium theme and MySQL database. The value in the database box looks like this:
a:1{i:0;a:4{s:4:"name";s:9:"Trailer";s:6:"select";s:6:"iframe";s:6:"idioma";s:2:"en";s:3:"url";s:82:"https://youtube.com/sample.mp4
";}}
When I edit the YouTube link value to something else, the entire data in this box disappears on the live Wordpress page, although it is visible in the database even after refresh. I have no idea why this happens and how I can keep it from happening.
EDIT:
After i tried editing other values like post_title etc it just wont update the values at all on the live WP page.Why im doing this is because i need to add and edit mass amounts of data easily with scripts.
The string you are displaying is coming from PHP serialize. This is a way for PHP to stringify any value for later usage.
If you want to mass modify those values, your best bet is to create a PHP script that fetch the data, unserializes it, make change directly to the PHP variable it created, and serialize again to put to database.
If you want to play with the string directly, you will need to make sure you are careful.
The main reason why changing the URL of the youtube video doesn't work is because you might not be changing the string declaration too.
s:82:"https://youtube.com/sample.mp4";
This is invalid. It is split into 3 parts, using :. Type:Length:Value. So it is a string of length 82, yet you provide a 30 character string.
If you turn on NOTICE in PHP you will certainly see the errors about it.
EDIT:
After tinkering a bit on PHPFiddle.org I came up with a clean string from the one you gave, which has numerous flaws...
a:1:{i:0;a:4:{s:4:"name";s:7:"Trailer";s:6:"select";s:6:"iframe";s:6:"idioma";s:2:"en";s:3:"url";s:30:"https://youtube.com/sample.mp4";}}
Note that I changed the Length values in 2 parts, and added 2 semi-colon :

WordPress Migration - Trouble with Import of MySQL Dump

I'm currently moving sites from one server to another. In doing this, I'm setting up new dev environment to help test any issues we may have in moving the site.
I've already moved one site, quashing all issues. I've had some issues with the latest two sites.
My work flow normally is:
1) Export
2) Edit to dev URL in Sublime Text via find/replace
3) Import to DB
Importing a MySQL dump first gave me an error such as
"ERROR at line XXX: unknown command YYY"
In one such scenario, this seems to be caused by an encoding issue (STX) for some unknown character.
I can get such errors to go away by using MySQL flags such as:
-default-character-set=latin1 for the mysqldump and NOT using --default-character-set=utf8 with the import.
This gets me further in the process, as things seem to be working, except no images show in Media (just default.png) for the thumbs. I've verified that the files exist on the server and checked permissions. When clicking on an image in Media, I can copy/paste the URL and the image shows properly in a browser.
Another example of where the data isn't fully transferring is that the template used for a page isn't being retained.
I'm at a loss at this point on how to properly export/format/import the MySQL dump. I've even tried the WordPress repair database utility without any change in results.
O. Jones comment mentions a plugin called Duplicator. I didn't utilize this given our situation so I cannot speak to whether or not it would. If that would work correctly, I would imagine that the ideal solution for most.
The solution that I've utilized is simple but may not work for those without access to or unfamiliar with MySQL.
The steps are:
Export via "mysqldump -u {user} -p {databaseName} >
{filename.sql}
Import like usual (without issues)
Log into the new MySQL database where the import was completed, use that database and run the following queries:
UPDATE wp_options SET option_value = replace(option_value, '{oldURL}', '{newURL}');
UPDATE wp_posts SET guid = replace(guid, '{oldURL}', '{newURL}');
UPDATE wp_posts SET post_content = replace(post_content, '{oldURL}', '{newURL}');
UPDATE wp_postmeta SET meta_value = replace(meta_value, '{oldURL}', '{newURL}');
These four queries may need to be run for each variation of "http://oldwebsite.com", "http://www.oldwebsite.com", "https://oldwebsite.com", and "https://www.oldwebsite.com".

How to replace bulk href link in mySql database

I have many posts in wordpress. In that post I have links like dev.dev.example.com now I want it to replace with dev.example.com.
I am using following query
UPDATE `dbname`.`wp_posts` SET `post_content`= replace(cast(post_content as varchar(max)), '%dev.dev.example.com%', '') WHERE CONVERT(`post_content` USING utf8) LIKE '%dev.example.com%'
I think it is better to follow wordpress' official guide when moving a wordpress website. http://codex.wordpress.org/Moving_WordPress
Alternatively, as an easy way in your case if you have already done all other steps but stuck in this issue, you can export database to sql file, open in text editor like notepad, use find-and-replace to replace all x.com to y.tom and then import the database. But remember that if domain length is different, you will have some settings lost. It is because wordpress keeps serialized data where length is stored.
Using your way to do via query is not a robust way because some other tables may still have old domain name.

Laravel 4 - Reversing Markdown for Textarea Editing

I am using a Markdown package inside my Laravel 4 application to store user input from textareas into my DB.
https://github.com/vtalbot/markdown
This works perfectly using:
Markdown::string('#test');
However, when I edit a record and set the value of a textarea to the existing data in the DB (the data that was formatted for entry), elements such as P tags are shown, which isn't ideal.
Does anyone know of a way to "reverse" markdown for this? Maybe I should be approaching this differently?
Thanks.
You don't reverse it. You store the unparsed version in the database and parse it when you display it to the user. And if you want to save on some overhead when parsing then you can cache it in some form. Either via an actual cache, like redis or memcached, or you store it in an additional field in the database and update the parsed version whenever it is updated.

PHP Serialized database entries messing me all up

O.K. so I'm pretty clever - I've made a library for keeping a bunch of WP themes that are all set up to my needs so when I put out a new site I can just create the new blog in a few minutes.
As a holder for the new domain everything in the sql file that has the old domain in it I replace with [token].
Everything was working fine right up to the point where I made one with a child theme that apparently serialized data before entering it into the database. End up with stuff like this:
Theme','a:8:{s:12:\"header_image\";s:92:\"http://[token]wp-content/uploads/2011/05/494-Caring-for-fruit-trees-PLR.jpg\";s:16:\"background_image
So I dig into serialization and it turnss out that s:92 bit for example is the number of characters in that value. Since i'm changing [token] it changes and breaks.
Up till now I did all my changes to the sql file and didn't edit the database other than to populate it with the sql file - but I'm at a loss on how to deal with the data in the serial array.
Any ideas?
Easiest way would be to grab that data and use the unserialize() function, like
$arr = unserialize($data);
Then edit the data that way. When you're done, re-serialize it with serialize(), and store it back. You may have to to do a print_r() on the unserialized data to see how it's stored to see what you need to edit.
If you do the changes directly from the serialized data, you'll have to get the length of the current substring, make the change, then get the new length and splice that back into the serialized data, which is way more complicated than it needs to be.

Categories