• Skip to content
  • Skip to primary sidebar

Archives for January 2019

Programmatically Set Reading Settings in WP Admin

January 11, 2019 by edwinvelez Leave a Comment

To set a Home Page and Post Page, changes are normally submitted manually in the Reading Settings page of the WP Admin Dashboard. With the code snippet below, these changes can be made programmatically.

The two variables, $home_page and $blog_page, should match the title of a Page you create in the WP Dashboard.

<?php
/**
* Update WordPress Reading Settings Programmatically.
*
* @author Edwin Velez
* @see https://edwinvelez.net/programmatically-set-reading-settings-in-wp-admin/
*/
// Set static front page.
$home_page = get_page_by_title('Home');
update_option('page_on_front', $home_page->ID);
update_option('show_on_front', 'page');
// Set blog page.
$blog_page = get_page_by_title('Blog');
update_option('page_for_posts', $blog_page->ID);
view raw functions.php hosted with ❤ by GitHub

Filed Under: Code Snippets, WordPress

Compare A Domain Name Or Hostname with A WordPress Site

January 8, 2019 by edwinvelez Leave a Comment

The code below is able to compare a domain name or hostname with a WordPress site. What exactly does this mean?

WordPress has the built-in function get_site_url() which returns the URL for a given WordPress site. Imagine you have two sites. One site is for development work or staging design. The other is publicly available on the Internet and is the gold standard for all your hard work. What if certain lines of code should only run if in one environment but not the other? This is where comparing a domain name or hostname with a WordPress site can be useful.

In the code below, the variable $live_site_url should point to your live site. Again, your live site is the domain name, i.e. example.com, where your WordPress instance can be found on the Internet. Finally, add your lines of code within the if block.

<?php
/**
* Compare A Domain Name Or Hostname with A WordPress Site
*
* @author Edwin Velez
* @see https://edwinvelez.net/compare-a-domain-name-or-hostname-with-a-wordpress-site/
*/
// The domain name or host you want to compare. Can also be a host i.e. http://localhost
$live_site_url = 'https://edwinvelez.net';
$head_pattern = '/(https?:\/\/(w{3}\.)?)?(^w{3}\.)?';
$tail_pattern = '(\:[0-9]+).*|\b\/.*|\b\?.*';
$head_vs_tail_boolean = '|';
$regex_flags = '/m';
$regex_pattern = $head_pattern . $head_vs_tail_boolean . $tail_pattern . $regex_flags;
// Strip everything before and after actual domain name and hostname.
$live_site_url = preg_replace( $regex_pattern, '', strtolower( $live_site_url ) );
$site_url = preg_replace( $regex_pattern, '', strtolower( get_site_url() ) );
$is_live_site = strcmp( $site_url, $live_site_url ) === 0;
// Is the web browser pointing to the live site?
if ( $is_live_site ) {
// Insert code here to run if this is your live site.
} else {
// Insert code here to run if this is not your live site.
}
view raw functions.php hosted with ❤ by GitHub

Filed Under: Code Snippets, WordPress

Primary Sidebar

Recent Posts

  • Convert A WordPress Menu To A Bootstrap 4 Menu
  • Programmatically Set Reading Settings in WP Admin
  • Compare A Domain Name Or Hostname with A WordPress Site

Recent Comments

    Archives

    • February 2019
    • January 2019

    Categories

    • Code Snippets
    • WordPress

    Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

    Copyright © 2019 · Edwin Velez on Genesis Framework · WordPress · Log in