-1

I want to execute multiple different PHP snippets which all retrives 1 page ID (yes, I want to extend the code and display some other things then the page ID).

But if I execute 2 or more snippets at the same time, I get a blank page...

My question is, how can I fix this error and why and how does this error get caused?

This is what I get from the error log:

[Wed Oct 11 08:14:06.322845 2017] [proxy_fcgi:error] [pid 5616:tid 140278597940992] [client xx.xxx.xxx.xxx:xxxxx] AH01071: Got error 'PHP message: PHP Fatal error: Cannot redeclare getPages() (previously declared in /var/www/vhosts/classymagazin.de/httpdocs/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(29) : eval()'d code:5) in /var/www/vhosts/classymagazin.de/httpdocs/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(29) : eval()'d code on line 12\n'

This is the PHP code for the latest page:

<?php

    function getPages(){
        $args = array(
        'sort_order' => 'desc',
        'sort_column' => 'post_date',
        'post_type' => 'page',
        'number' => '26',
        'post_status' => 'publish');

        return get_pages($args);
    }

    function filterPages($allNews, $pages){
         foreach($pages as $page) {
                $pid = $page->ID;
                $object = get_the_category($pid);
                $termsForPost = array();

                foreach($object as $terms) {
                    if($terms->term_id == 15 || $terms->term_id == 16){
                        $termsForPost[] = $terms->term_id;
                    }
                }

                if(in_array(15, $termsForPost) && in_array(16, $termsForPost)){
                    $allNews[$pid] = array("ID" => $pid, "Sticky" => true, "Timestamp" =>  get_the_time('U', $pid), "Title" => get_the_title($pid));
                }else if(in_array(15, $termsForPost)){
                    $allNews[$pid] = array("ID" => $pid, "Sticky" => false, "Timestamp" => get_the_time('U', $pid), "Title" => get_the_title($pid));
                }
            }

        return $allNews;    
    }

    function sortPages($allNews){
            $stickyPosts = array();
            $nonStickyPosts = array();

            foreach($allNews as $article){
                if($article["Sticky"] == true){
                        $stickyPosts[] = $article["ID"];
                    }else{
                        $nonStickyPosts[] = $article["ID"];
                    }
            }

            $allNews = null;

            foreach($stickyPosts as $tmp){
                $allNews[] = $tmp;
            }

            foreach($nonStickyPosts as $tmp){
                 $allNews[] = $tmp;
            }

        return $allNews;
    }

    function getAllArticle($index = 0){
        if($index == 0){

            $allNews = array();
            $pages = getPages();

            $allNews = filterPages($allNews, $pages);
            $allNews = sortPages($allNews);

            foreach($allNews as $article){
                var_dump($article);
                echo "<br>";
            }

        }else{

            $allNews = array();
            $pages = getPages();

            $allNews = filterPages($allNews, $pages);
            $allNews = sortPages($allNews);

            var_dump($allNews[$index - 1]);
        }
    }



    getAllArticle(1);

?>

Kind regards

  • What does your error log tell you? If you don't know how to set up debugging, you can start with codex.wordpress.org/Debugging_in_WordPress. Also, it's not clear how you initiating the script. – CK MacLeod Oct 11 '17 at 6:38
  • I have updated the question! – Jan Oct 11 '17 at 6:44
  • never use such tools, write proper code, use GIT to manage your changes – Mark Kaplun Oct 11 '17 at 6:53
0

Maybe I'm misunderstanding something, and it's still not clear to me how you're using this code, but it seems like a straightforward PHP error. You can't declare an independent function with the same name twice. You declare (or define) the function with the function function_name() only once, and then you can call it by name. The error message suggests that you've declared the function once at line 5 in the indicated file, and against at line 12.

0

CK MacLeod is correct. Not big matter. Basically out of that error, total logical structure has been impossible to guess for us! So you have to correct it and check.

Possibly you'll like to install & configure IDE for WordPress like MS visual studio, configure that MS visual studio for Wordpress debugging. Although that will not catch gross mistakes yet you can easily view the function function_name() easily. And here is example of using endif on WordPress doc in complex snippets.

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.