0

I'm declaring a function on a plugin's PHP. The plugin loads code at the top of each post (Post Layout is the plugins name).

Cannot redeclare vote_up() (previously declared in /home/content/08/10290908/html/wp-content/plugins/post-layout/plugin.php(181) : eval()'d code:4) in /home/content/08/10290908/html/wp-content/plugins/post-layout/plugin.php(181) : eval()'d code on line 36

that's the displayed error message, it isnt because the function has already been declared in the functions.php (I know because I tested it by renaming it to all sorts of names) It says it's already declared in the plugin I'm using to load the code.

Heres the code:

<?php 

function vote_up(){
                    if($post_vote == "9"){

        }
        elseif($post_vote == "8"){
        update_post_meta( get_the_ID(), 'post_vote', '9', '8');
        }
        elseif($post_vote == "7"){
        update_post_meta( get_the_ID(), 'post_vote', '8', '7');
        }
        elseif($post_vote == "6"){
        update_post_meta( get_the_ID(), 'post_vote', '7', '6');
        }
        elseif($post_vote == "5"){
        update_post_meta( get_the_ID(), 'post_vote', '6', '5');
        }
        elseif($post_vote == "4"){
        update_post_meta( get_the_ID(), 'post_vote', '5', '4');
        }
        elseif($post_vote == "3"){
        update_post_meta( get_the_ID(), 'post_vote', '4', '3');
        }
        elseif($post_vote == "2"){
        update_post_meta( get_the_ID(), 'post_vote', '3', '2');
        }
        elseif($post_vote == "1"){
        update_post_meta( get_the_ID(), 'post_vote', '2', '1');
        }
        elseif($post_vote == "0"){
        update_post_meta( get_the_ID(), 'post_vote', '1', '0');
        }
            else{//do nothing
        };
}

//if the post is pending show
if(get_post_status() == 'pending') {

//if the user is logged in show
if ( is_user_logged_in() ) {

    add_post_meta( get_the_ID(), 'post_vote', '0', true );
    $post_vote = get_post_meta( get_the_ID(), 'post_vote', true );

    echo 'Votes: ' . $post_vote;

           echo ' <form action="xxx" method="post" onclick="return vote_up()">';
           echo ' <input type="button" value="Vote Up" />';
           echo '</form>';


} else {
    echo'Please Sign in to Vote';
}

} else {
// do nothing
}


?>

The code above is basically trying to make it so that when you click on the button, the vote meta_key goes up by one. It's for voting so once 10 users vote on it, it will publish.

This stupid buttons such a hassle, I can't move on until I have it working even tried to pay some guy to fix the button and he didn't have a clue

  • How is this code used? Where is that code? I am 99% sure, by the way, that I can fix this but I need to know how you are using the code. The short answer is "you can't load a file with function definitions more than once" but if I knew what you were trying to do I might have a positive answer instead of a negative one. – s_ha_dum May 25 '14 at 22:01
  • Do you mean what does my function do and my code do altogether? Or what the code that's in the PHP error message is? (that's the plugins code I think) – PirateScott May 25 '14 at 22:13
  • You say that "The plugin loads code at the top of each post..." How? And why does functions.php and the plugin have a function of the same name? – s_ha_dum May 25 '14 at 22:17
  • I'm not sure how it does it but I could show you the source code of the file of the plugin it's talking about? and Nope like I said in the post there aren't any similar functions in functions.php. – PirateScott May 25 '14 at 22:21
  • Is this your plugin? Are you writing it? If so, yes, please post more of the code. – s_ha_dum May 25 '14 at 22:23
0

You don't want to add function vote_up() { [...] } at the top of every post if you display more than one post on a page. You want to declare the vote_up() function in your plugin's PHP file.

Once the plugin declares the function, as long as the plugin is active, you can just call the function wherever you need it.

I would however suggest two further changes:

  • Prefix the function name.

vote_up is rather generic, and you run the risk of colliding with other plugins and/or themes. Something like [plugin_name]_vote_up() would work nicely.

  • Check to make sure the function exists before using it.

If you call vote_up anywhere outside the plugin (e.g. a theme template file), surround the call as follows:

if ( function_exists( 'vote_up' ) ) {
    vote_up();
}

This will ensure that your theme files will still function properly if you ever need/want to deactivate the plugin for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.