WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sorry-WP Went Whacky. I Need Examples of Using the "option_xxx" Filter. (3 posts)

  1. cmarshall
    Member
    Posted 7 years ago #

    For some reason, the posting went nuts, so you are graced with three copies of an empty post.

    Here's what I was trying to say:

    ---
    My last query got demoted from a support question (probably because it deserved to be).

    I got some good feedback, but now I am stuck.

    I normally look for example code to shine a cluelight upon my moronity, but there seems to be a dearth of examples for this.

    I wrote a plugin, as recommended, and set up a filter, as recommended, but the filter function is never called, which leads me to believe that there is some problem with the way I set it up. I can't see the forest for the trees. This is as simple a plugin as it gets, yet it doesn't work.

    Any ideas?

    The Code:

    <?php
    /*
    Plugin Name: localhosts
    Plugin URI: http://...
    Description: Makes things local, for testing.
    Version: 1.0
    */
    function GetMainURI( $ret )
        {
        if ( preg_match ( '|10\.211\.55\.2|', $_SERVER['REQUEST_URI'] ) )
            {
            $ret = "http://10.211.55.2/.../public_html/main";
            }
        elseif ( preg_match ( '|127\.0\.0\.1|', $_SERVER['REQUEST_URI'] ) || preg_match ( '|localhost|', $_SERVER['REQUEST_URI'] ) )
            {
            $ret = "http://localhost/.../public_html/main";
            }
        return $ret;
        }
    
    add_filter ( 'option_home', 'GetMainURI' );
    add_filter ( 'option_siteurl', 'GetMainURI' );
    ?>
  2. cmarshall
    Member
    Posted 7 years ago #

    Okay, I got it working.

    I remember someone once saying that filter/hook function names should always be lowercase, so that's what I did, and now it works just ducky.

    This could be a useful function for people who like to have test laptops with all kinds of virtual systems (like I do), so I'll share my very simple patcher. You will need to add your own local URIs:

    <?php
    /*
    Plugin Name: localhosts
    Plugin URI: http://...
    Description: Makes things local, for testing.
    Version: 1.0
    */
    function get_uri( $ret )
        {
        if ( preg_match ( '|10\.211\.55\.2|', $_SERVER['SERVER_ADDR'] ) )
            {
            $ret = "http://10.211.55.2/.../public_html/main";
            }
        elseif ( preg_match ( '|127\.0\.0\.1|', $_SERVER['SERVER_ADDR'] ) || preg_match ( '|localhost|', $_SERVER['REQUEST_URI'] ) )
            {
            $ret = "http://localhost/.../public_html/main";
            }
        return $ret;
        }
    
    add_filter ( 'option_home', 'get_uri', 10, 1 );
    add_filter ( 'option_siteurl', 'get_uri', 10, 1 );
    ?>
  3. cmarshall
    Member
    Posted 7 years ago #

    REALLY ANNOYING FOLLOW-UP:
    This won't work for admin. Admin re-loads straight from the DB, so it can't be patched. Probably more secure, but it means that you need to hack the core to use admin on local machines without going in and changing the database.

Topic Closed

This topic has been closed to new replies.

About this Topic