Created my first wp plugin but...

2 replies
Hello all,

I just finished up a cool wp plugin that I have been testing. The thing is that it displays no errors when I run the plugin or when I go to the url for the file itself...

The thing is that when I start running it for testing and come back later on that day or week I find its broken and not working, just shows front page of the site...

Any ideas?

Sincerely;

J

The code is below.

PHP Code:
<?
global $domain1;
global $domain2;
global $basews;
function eztest(){//works redirects properly using shortcode
    global $domain1;
    global $domain2;
    global $basews;
        $domain1=get_option('domain1');
        $domain2=get_option('domain2');
        $basews=get_option('basews');
        $ct = 0;
 if(!empty($domain1) || $domain1 != null){
   $ct=$ct+1;     
 }elseif(!empty($domain2) || $domain2 != null){
     $ct=$ct+1;
}
$test = rand(0,$ct);
if($test == 0){
    $location = $domain1;
}else{
    $location = $domain2;
}
header("Location: ".$basews.$location);
}
add_shortcode('eztest','eztest');

function esplit_activate(){//works creates the options and allows for data saved
    global $domain1;
    global $domain2;
    global $basews;
    add_option("domain1",null);
    add_option("domain2",null);
    add_option("basews",null);
}
register_activation_hook(ABSPATH.'/easy-split-test.php','esplit_activate'); // generates call to undefined function

function esplitd(){//works
    global $domain1;
    global $domain2;
    global $basews;
    delete_option("domain1");
    delete_option("domain2");
    delete_option("basews");
}
register_deactivation_hook('/easy-split-test.php','esplitd'); //works

function easy_split_tester(){//works
    add_options_page('Easy Split Tester', 'Easy Split Tester', 'manage_options', 'ez-split-tester', 'plugin_options');
}
add_action('admin_menu', 'easy_split_tester'); //works

function plugin_options(){
    //works
    global $domain1;
    global $domain2;
    global $basews;
    if (!current_user_can('manage_options'))  {
        wp_die( __('You do not have sufficient permissions to access this page.') );
    }
    if(isset($_POST['go'])){
        $domain1 = $_POST['domain1'];
        $domain2 = $_POST['domain2'];
        $basews = $_POST['basews'];
        update_option('domain1',$domain1);
        update_option('domain2',$domain2);
        update_option('basews',$basews);
?>
<div class="updated"><p><strong><?php _e('Update Successful!'); ?></strong></p></div>
<?
    }else{
        $domain1=get_option('domain1');
        $domain2=get_option('domain2');
        $basews=get_option('basews');
?>
<div class="wrap">
<?php echo "<H2>".__('Easy Split Tester Settings')."</H2>"?>
<form method="post" action="<?php echo str_replace('%7E','~',$_SERVER['REQUEST_URI']); ?>">
<input type="text" name="domain1" placeholder="Enter your first page" size="100" value="<?php echo $domain1?>" /><br>
<input type="text" name="domain2" placeholder="Enter your second page" size="100" value="<?php echo $domain2?>" /><br>
<input type="url" name="basews" placeholder="http://" size="100" value="<?php echo $basews?>" /><br />
<p class="submit">
<input type="submit" name="go" value="Save" />
</p>
</form>
</div>
<?
    }
}
?>
#created #plugin
  • Profile picture of the author Tim Brownlaw
    Here's a few pointers when it comes to WP Plugins.

    I'm guessing you've not fully read the WP Plugin Developers guide, You Should have another read of - Writing a Plugin.

    Watch the naming of your functions and options. I don't see you using the recommended way of doing things. Yes, this might be a simple test and just for your blog but it's a good habit to fall into early.

    Just to let you know, I am running this in one of my test blogs I use for plugin development.

    Now you are attempting to perform a redirect after the page has been rendered and displayed

    Did you say that it "works" when you do something and then later it doesn't?

    Issuing a header("location:.... ) after the page has been rendered and output, will produce the ole "headers already sent by....." Which is what I am seeing and no redirect is happening!

    So what is the "something"? I'm unsure as to what you are doing.

    There is a reason it's not working ( of course ), so have a think!

    Cheers
    Tim
    {{ DiscussionBoard.errors[5160869].message }}
  • Profile picture of the author eminc
    Hi,

    I can guess from quickly looking at the code that you are doing some kind of split testing for posts or pages. You have defined a shortcode, and by using that you are randomly deciding which link to redirect to.

    As Tim suggested, its not wise to use PHP Redirect after the content has been delivered to the web page, and before a post most of the content is already been sent to the HTML page.

    You can do a workaround by redirecting it using Javascript (probably). So instead of header(location: ) you can write

    echo "<script>window.location='".$ location."'</script>";
    Hopefully it should help.


    Mohit
    {{ DiscussionBoard.errors[5161719].message }}

Trending Topics