WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Sorry-WP Went Whacky. I Need Examples of Using the “option_xxx” Filter.

[Resolved] Sorry-WP Went Whacky. I Need Examples of Using the “option_xxx” Filter.

  • 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' );
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • 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 );
    ?>

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Sorry-WP Went Whacky. I Need Examples of Using the “option_xxx” Filter.’ is closed to new replies.
Skip to toolbar