Setting home and siteurl dynamically in Wordpress

Setting home and siteurl dynamically in Wordpress

Created:10 Dec 2016 09:44:06 , in  Web development

Use wp_config.php or functions.php to set home and siteurl dynamically.

Hard-coded constants

In wp-config.php.:


define('WP_SITEURL', 'http://<your.domain.here>/');
define('WP_HOME', 'http://<your.domain.here>/'); 

If these two constants are set the _config_wp_home and _config_wp_siteurl will be used by WordPress to overwrite the settings in the database.

Custom filters

Alternatively, you can dynamically set your home and siteurl options by attaching your own callbacks to option_home and option_site filters in functions.php of your theme.


function my_filter_option_siteurl( $siteurl ) {
  // filter $siteurl here   
  return $siteurl; 
}; 
             
// add the filter 
add_filter( 'option_siteurl', 'my_filter_option_siteurl');

function my_filter_option_home( $home ) {
  // filter $home here 
  return $home; 
}; 
             
// add the filter 
add_filter( 'option_home', 'my_filter_option_home', 10, 1 );
 

This post was updated on 06 Oct 2021 20:28:34

Tags:  php ,  wordpress 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.

Copyrights

©Copyright, 2024 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "Setting home and siteurl dynamically in Wordpress." From sWWW - Code For The Web . https://swww.com.pl//main/index/setting-home-and-siteurl-dynamically-in-wordpress

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]


I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!