For doing local development on a WordPress site (http://www.example.com), I was previously overriding the WP_SITEURL and WP_HOME values in wp-config.php like so:
define('WP_SITEURL', 'http://local-example/');
define('WP_HOME', 'http://local-example/');
This would allow me to copy the database and site files to a local server, and make modifications as necessary, testing on the local install.
It was then necessary to convert the install to a WordPress Multisite so that users, authentication, plugins, etc. could be shared between the main site and a secondary site, hosted on a subdomain (http://second.example.com).
The method above to override the values in the wp_options table no longer works, but I am unsure the proper way to set a value for the entries in wp_blogs as well as the wp_2_options table for the primary and subdomain.
Updating my HOSTS file is somewhat of a workaround, but it not ideal (I am not able to compare to the live site, etc). Running a script to change the database values is another option I have tried, but is slightly more cumbersome, so my questions is whether or not there is an option in MultiSite to override these values in a settings file, such as wp-config.php, and if so what it would look like.
Update: full updated plugin code with additional description can be found here: http://justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-wp-server-migration/
I was able to come up with a solution with the help of #user916011. I needed to be able to copy the wp_options table(s) to my development environment as they contain configurations that are needed. To overcome the issue of not being able to set the WP_SITEURL and WP_HOME values in MultiSite, I wrote a custom filter to replace the _config_wp_siteurl() and _config_wp_home() functions that are available for non-multisite installs that is included in a plugin that is available network-wide and is configured in wp-config.php. I am then able to copy all of the database tables except wp_site and wp_blogs to a local database.
I highly recommend the URL Token Replacement Techniques for WordPress 3.0 article by Chris Murphy to help handle URLs in your content.
This example assumes a subdomain multisite install, with a domain of example.com and two subdomains, www.example.com and second.example.com. The local development URLs will be www.example.local and second.example.local respectively.
Database Changes:
Update the domain value in wp_site:
UPDATE wp_site SET domain = 'example.local' WHERE domain = 'example.com';
Update the domain value(s) in wp_blogs:
UPDATE wp_blogs SET domain = 'www.example.local' WHERE domain = 'www.example.com';
UPDATE wp_blogs SET domain = 'second.example.local' WHERE domain = 'second.example.com';
Plugin Code:
The following plugin should be installed network-wide.
<?php
/*
Plugin Name: MultiSite WP_HOME and WP_SITEURL
Plugin URI: http://doublesharp.com/
Description: Allows wp_options values to be overwritten in wp-config.php for MultiSite
Author: Justin Silver
Version: 1.0
Author URI: http://doublesharp.com
License: GPL2
*/
function _ms_config_wp_siteurl( $url = '' ) {
if (is_multisite()):
global $blog_id, $current_site;
$cur_blog_id = defined('BLOG_ID_CURRENT_SITE')? BLOG_ID_CURRENT_SITE : 1;
$key = ($blog_id!=$cur_blog_id)? $blog_id.'_' : '';
$constant = 'WP_'.$key.'SITEURL';
if ( defined( $constant ) )
return untrailingslashit( constant($constant) );
endif;
return $url;
}
add_filter( 'option_siteurl', '_ms_config_wp_siteurl' );
function _ms_config_wp_home( $url = '' ) {
if (is_multisite()):
global $blog_id;
$cur_blog_id = defined('BLOG_ID_CURRENT_SITE')? BLOG_ID_CURRENT_SITE : 1;
$key = ($blog_id!=$cur_blog_id)? $blog_id.'_' : '';
$constant = 'WP_'.$key.'HOME';
if ( defined( $constant ) )
return untrailingslashit( constant($constant) );
endif;
return $url;
}
add_filter( 'option_home', '_ms_config_wp_home' );
?>
Configure wp-config.php:
Add new constants to wp-config.php. The primary site should use the standard WP_HOME and WP_SITEURL and the tertiary URLs should use WP_{$blog_id}_HOME and WP_{$blog_id}_SITEURL
define('WP_HOME', 'http://www.example.local');
define('WP_SITEURL', 'http://www.example.local');
define('WP_2_HOME', 'http://secondary.example.local');
define('WP_2_SITEURL', 'http://secondary.example.local');
You could use the update_option in functions.php
update_option("siteurl","http://example.com");
update_option("home","http://example.com");
There's a similar question being asked here: Team Development of a Wordpress Site which I provided a possible solution for. In your case, you may not want to go to that extent (though it would be very flexible); however, you could always look at the portion of the answer that mentions a domain-replacement technique.
I've outlined that solution here: URL Token Replacement Techniques...
I had the same issue and wanted a solution as similar to defining WP_HOME & WP_SITEURL in wp-config.php as possible.
I can't use a plugin, because I am syncing with GIT and don't want to have that plugin in my repo and I would have to add the plugin everytime... I suppose it could be activated network wide through the wp_sitemeta table... but it wasn't ideal for me.
I came up with this solution.
Be sure to not sync wp_blogs, wp_site, wp_sitemeta. And then add this code to your local wp-config.php somewhere below $table_prefix:
/* Update local database */
mysql_connect( DB_HOST, DB_USER, DB_PASSWORD ) or die( mysql_error() );
mysql_select_db( DB_NAME );
$result = mysql_query("SELECT * FROM `". $table_prefix ."blogs`") or die( mysql_error() );
while( $row = mysql_fetch_array( $result ) )
{
$id = (1 == $row['blog_id']) ? '' : $row['blog_id'] .'_';
mysql_query( "UPDATE `". $table_prefix . $id ."options` SET `option_value`='http://". $row['domain'] ."/' WHERE (`option_name`='siteurl' OR `option_name`='home')" ) or die( mysql_error() );
}
This will make sure your sites are synced up to your local wp_blogs table.
The only drawback is that when you add a new site, you do manually need to copy it into your wp_blogs table and update its local url.
This question is too old to answer, but recently I faced a similar situation where I have to migrate my wp site from staging to production and it was painful as it is having 5 subsite.
After scratching my head for all day, I found a simple and useful solution.
Of course one have to make changes in wp-config.php.
Other then that you have to follow below steps.
Generate database dump using following command.
mysqldump -u [user name] –p [password] [options] [database_name] [tablename] > [FileName]
After that open that file in any editor.
Find and replace your old domain with new domain.
Now restore the database with dump you created.
mysql -u [user name] -p [password] [database name] < [file anme]
Related
I have some categories on a site with over 350,000 active posts some of which have as many as 50,000 posts. The volume makes using a bulk delete plugins not viable because they all timeout and delete nothing. The best feedback I've gotten from the Wordpress support forum is to use WP-CLI but beyond that all they've done is give me a link to the WP-CLI website which contains no example specific to my needs.
I have never used CLI before beyond a failed attempt to run Media Cleaner which resulted in a timeout at 999 seconds. That was months ago so I forget how I tried to do that. Could someone please post a code snippet that could basically be copied and pasted into CLI with the only change I need to make being swapping the category ID or date/ID number of the oldest post I want to keep?
Again, I want to be able to delete all posts from a specific category or before a specific date/ID.
Alternatively, could someone post a SQL statement so that I can do this using MySQL without installing CLI? The SQL would be better because I could apply it to any site without installing let alone learning CLI.
You don't have access to phpmyadmin?
You could avoid all of that just by deleting posts on load:
/**
* REMOVE EVIL POSTS IF AND WHEN LOADED
* add to functions.php
* works on regular posts and simple categories,
* check codex for details if custom post types, custom taxonomies, etc.
**/
add_action( 'template_redirect', 'delete_post_on_load' ) ;
function delete_post_on_load() {
global $post ;
//has term works more consistently if all variables are supplied
if ( has_term( array( 'Category to Go', 'Second Category to Go' ), 'category', $post->ID ) ) {
wp_delete_post( $post->ID, true ) ;
wp_safe_redirect( site_url() ) ; //redirect to home instead of 404 on deletion
exit() ;
}
}
I have created a section in my functions.php file and registered a shortcode which is intended to echo some data from a custom mysql database table.
I have an external system which will insert data into the database.
My wordpress install should be able to read from this database, and I would like to echo some rows on a wordpress page via shortcode.
The problem I am having is that the table data is not being echoed out. I can do echo "test"; and the word test will get displayed but the problem I having is not showing table data. Heres my code.
function vstp_feed_function(){
//$x++;
//$alternatingclass = ($x%2 == 0)? 'whiteBackground': 'graybackground';
$mydb = new wpdb('root','cencored','testdb','localhost');
$rows = $mydb->get_results("select * from services");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->headcode."</li>";
endforeach;
echo "</ul>";
}
add_shortcode('vstp_feed', 'vstp_feed_function');
So the above code will create a new variable mydb, with the database credentials and information. A new variable called $rows will store the sql query which uses the mydb variable. I get all columns from services table. Thereafter I am echoing an unordered list, running a for loop to display the contents of the column "headcode" in list item html.
Once the select query is done, the for loop ends and so does the html list.
The shortcode is registered as [vstp_feed] which is tested and working with static html.
Any help would be greatly appreciated. Worked with sql before but not with new wordpress db instances. Thanks.
Just to confirm, its apache ubuntu. Its also worth noting that my ##datadir is stored on a different disk seperate from my OS disk, for faster IO and freedom to expand storage on the fly
Place your connection code inside wp-config.php file ( $mydb = new wpdb('root','cencored','testdb','localhost'); )
And use it in your function file as "global $mydb;"
Does anyone know how to separate wp_options from two WordPress while sharing post data?
I’m now creating websites both personal and my company’s one using WordPress. I realized that it will be efficient if I could share post data on both websites which have different web design.
Although I’ve already tried sharing MySQL and it works, it also shared title name which stored in wp_options as well. That means if I changed site name of the personal website, it also effects company’s one. This is not only about site name but other settings in wp_options.
I also couldn't find how to do this from official WordPress website below.
https://codex.wordpress.org/Editing_wp-config.php
Yes, we can separate wp_options for two WordPress while sharing post data
Install 2 website with single db ( 2 copies ).
Create new table in you'rs db. Call it wp_options2 and copy everithing from wp_options inside new table
in seccond install in wp-config.php, before if ( !defined('ABSPATH') ) add define( 'M7_OPTIONS_TABLE', 'wp_options2' );
in seccond install go to wp-includes/wp-db.php on line 951 and add code:
if ( isset( $tables['options'] ) && defined( 'M7_OPTIONS_TABLE' ) )
$tables['options'] = M7_OPTIONS_TABLE;
( these code should be added in public function tables function, before if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) )
Now you should be able to change theme, plugins atc. for seccond theme, but all posts, taxonomies atc. will be dubbled in both websites ( wich is bad for seo bdw. )
if on seccond install you'll have some problems with links, add
define('WP_HOME','http://seccond_website.com');
define('WP_SITEURL','http://seccond_website.com');
in your wp-config.php file .....
On plugin activation, I have 4 tables that are created in the WordPress database.
On uninstall I'd like to have the tables deleted.
I've been able to write it out to delete the tables on deactivation. But from what I've read, it's better to keep the database tables information until the admin uninstalls rather than deactivates.
I've been looking for answers but all I seem to able to find is dropping them on deactivation. I do also have the version option I need to uninstall with it.
You should not make uninstall.php as bhuthecoder said.
Using an uninstall.php file to handle plugin uninstallation can be problematic because it relies on the plugin being properly installed and activated before it can be run. If the plugin was never activated, or if it was deactivated and then uninstalled, the uninstall.php file will not be run and any cleanup tasks that it was responsible for will not be performed. This can lead to plugin-specific data being left behind in the database, which can cause problems if the plugin is re-installed at a later time. A better approach is to use a WordPress uninstall hook.
You can name the file like you want or you can do that in single file without separate file for uninstallation.
register_uninstall_hook('uninstall.php', 'on_uninstall');
It means that WP will run uninstall.php and function on_uninstall in the uninstall.php when the plugin will be deleted. So, you can rename the file if you want, you can rename the function.
register_uninstall_hook(__FILE__, 'on_uninstall');
It means the same, but __FILE__ indicate current file which is working now and function on_uninstall should be in this file.
FILE php.net
The full path and filename of the file with symlinks resolved. If used
inside an include, the name of the included file is returned.
Simple example of functions for activation/deactivation/uninstallation.
function on_activation()
{
//Some stuff
}
function on_deactivation()
{
//Some stuff
}
function on_uninstall()
{
//Some stuff
}
register_activation_hook(__FILE__, 'on_activation');
register_deactivation_hook(__FILE__, 'on_deactivation');
register_uninstall_hook(__FILE__, 'on_uninstall');
If you added some options you can delete it when uninstall like this:
delete_option( 'some name' );
As bhuthecoder said, you should not drop tables when deactivation, it is redundant and not friendly for a user, who can lost his data after deactivation.
There are syntax mistake in your code:
$sql = 'DROP TABLE IF EXISTS $table_name;';
Should be like this:
$sql = "DROP TABLE IF EXISTS $table_name";
If you want to put some variable in the string you should use double quotes.
And require_once is redundant.
Better like this:
function e34s_db_clients_uninstall()
{
global $wpdb;
$table_name = $wpdb->prefix . 'e34s_clients';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option('e34s_time_card_version');
}
register_uninstall_hook(__FILE__, 'e34s_db_clients_uninstall');
Or like this:
register_uninstall_hook('uninstall.php', 'e34s_db_clients_uninstall');
With function e34s_db_clients_uninstall in the uninstall.php.
there are two ways to do this operation on plugin uninstall/delete.
the best way i learned is by using uninstall.php
uninstall.php only loads when a plugin is removed or deleted from the plugins you can put any code in it to run when user deletes or uninstalls a plugin.
<?php
/*
* Removing Plugin data using uninstall.php
* the below function clears the database table on uninstall
* only loads this file when uninstalling a plugin.
*/
/*
* exit uninstall if not called by WP
*/
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit();
}
/*
* Making WPDB as global
* to access database information.
*/
global $wpdb;
/*
* #var $table_name
* name of table to be dropped
* prefixed with $wpdb->prefix from the database
*/
$table_name = $wpdb->prefix . 'table_name_to_be_dropped';
// drop the table from the database.
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
change the "table_name_to_be_dropped" by your table name.
Well, I got many results when I googled this issue. Here is what I got:-
function mr_np_activate(){
// hook uninstall
if ( function_exists('register_uninstall_hook') )
register_uninstall_hook(__FILE__,'mr_np_uninstall');
}
register_activation_hook(__FILE__,'mr_np_activate');
function mr_np_uninstall() {
delete_option('my_plugins_options');
}
source: https://wordpress.stackexchange.com/questions/24600/how-can-i-delete-options-with-register-uninstall-hook
For more info: http://codex.wordpress.org/Function_Reference/register_uninstall_hook
Use, register_uninstall_hook.
It Registers the uninstall hook that will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself,
for details check http://codex.wordpress.org/Function_Reference/register_uninstall_hook
Drop tables only when admin uninstall/delete the plugin.
Don't drop tables when admin deactivates the plugin , as Users normally deactivates all plugins to troubleshoot the wordpress errors after fixing they will reactivate the plugins again,if you drop tables at that time they will loose all your plugin settings that configured by admin .Also while upgrading wordpress automatically deactivates all plugins and reactivates after successful upgrade.
Instructions to delete tables while unistalling:
1)First you need to create unistall.php in your plugin root folder.This file is automatically called by wordpress when user clicks delete button.You can include your code in this file.
2)Must read full documentation about this in http://codex.wordpress.org/Function_Reference/register_uninstall_hook
use register_deactivation_hook. thats work for me.
function on_deactivation() {
global $wpdb;
$table_name = $wpdb->prefix . 'school';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query( $sql );
delete_option( 'wp_install_uninstall_config' );
}
register_deactivation_hook( __FILE__, 'on_deactivation' );
My wordpress site was recently hacked so I had to reinstall everything. Wordpress resinstalled, database backups imported, everything fine and dandy. Then I installed the Disqus plugin and synced (Disqus was previously used before the site was hacked). Big no-no apparantly. Now I have a duplicate of every single comment on my site! Even worse, the duplicate comments have been synced BACK to Disqus!
So, I know this is a simple query in PHPMyAdmin but I don't know it! Please help!
Keep in mind that there is a WordPress Stack Exchange website ;)
I would use PHP unless you know the wpdb name off the top of your head. Be sure to back up the DB first! Like so:
global $wpdb;
$comments = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."_comments"
." ORDER BY comment_post_ID, comment_content");
$prev = NULL;
foreach($comments as $comment) {
if ($prev && $prev->comment_content == $comment->comment_content
&& $prev->comment_post_ID == $comment->comment_post_ID ) { // add maybe other rules here
$wpdb->query("DELETE FROM ".$wpdb->prefix."_comments" WHERE comment_ID == $comment- >comment_ID");
}
else
$prev = $comment;
}
If he doesn't know how use mysql, I think re editing the wp interface is great, because usually wp creating there build function for queries. And it may lead again to destruction of it.
The only reason why it doesn't work maybe its because of the plug in they use.! I think so.!
The query for deleting in mysql is
Delete from (tablename) where (primarykey)
But I think wordpress has its build in database queries for select delete and update.
Here's another if you use the plugin Disqus
If you're logged into your Disqus account you can also choose to delete a comment at your dashboard. This will remove it from your profile and remove all identifying information from the comment on the original page. Once a comment has been anonymized it cannot be claimed again.