Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom pagination using a form with the get action so I can have a page jumper box. Everything works until you enter something that is not an actual page. Examples are negative numbers, text or a number greater than the actual number of pages.

What I am trying to do is intercept the variable and redirect the page if the page doesn't exist.

This is in my functions.php

add_action('init', 'k2wp_pagination');
function k2wp_pagination($k2url = '', $pages = '', $range = 2) {

    $showitems = ($range * 2) + 1;

    global $paged;

    if(empty($paged)) $paged = 1;

    if($pages == '') {
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if(!$pages) {
            $pages = 1;
        }
    }

    if(1 != $pages) {
        echo '<div class="pagination-wrapper clearfix">';
        echo '<form action="' . get_bloginfo('url') .'/'. $k2url . '" class="pagination-buttons" method="get">';
        echo '<div class="pager-buttons btn-group">';
        if($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo '<button value="1" name="paged" class="btn">&laquo; First</button>';
        if($paged > 1 && $showitems < $pages) echo '<button value="' . ($paged - 1) . '" name="paged" class="btn"><i class="icon-angle-left"></i></button>';

        for ($i=1; $i <= $pages; $i++) {
            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
            echo ($paged == $i)? '<button value="' . $i . '" name="paged" class="btn disabled" disabled="disabled">' . $i . '</button>' : '<button value="' . $i . '" name="paged" class="btn">' . $i . '</button>';
            }
        }

        if ($paged < $pages && $showitems < $pages) echo '<button value="' . ($paged + 1) . '" name="paged" class="btn"><i class="icon-angle-right"></i></button>';  
        if ($paged < $pages-1 && $paged + $range - 1 < $pages && $showitems < $pages) echo '<button value="' . $pages . '" name="paged" class="btn">Last &raquo;</button>';
        echo '</div>';
        echo '</form>';
        echo '<form action="' . get_bloginfo('url') .'/'. $k2url . '" class="pagination-jump" method="get">';
        echo '<div class="pager-jump">Page <input alt="Enter page to which you wish to jump" name="paged" type="text" value="' . $paged . '" class="jumper"> of ' . $pages . ' <button class="btn">Go</button></div>';
        echo '</form>';
        echo '</div>';
    }
}
share|improve this question
    
Post your code please. –  s_ha_dum Jul 23 '13 at 22:53
    
@s_ha_dum Done. –  kel Jul 23 '13 at 23:50
add comment

1 Answer

In your function, you can use $_GET and do all the validation you want to:

  • You can look if it's greater than 0
  • if it's a string or an integer

    And then use wp_redirect to go the error page or whatever page you want.

share|improve this answer
    
An example would really help. –  toscho Jul 24 '13 at 6:56
    
Adding an if statement to see if the form has been submitted works but redirecting doesn't work. –  kel Jul 24 '13 at 19:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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