How to deal with equal & similar arguments for a function?

I’m currently extending my pagination plugin, after a hint by @toscho. So the plugin will now use the same filters, hooks and arguments as the native wordpress pagination functions.

My problem is that the different wp functions name their arguments … different.

Read More

Example:

This is how you change the text for the “Next” link.

  • wp_link_page() = 'nextpagelink'
  • paginate_links() = 'next_text'

After retrieving the input in the class, I wp_parse_args( $args, $this->defaults ); my arguments and then extract them.

Problem:

When I now allow both 'nextpagelink' & 'next_text' as valid arguments, how would i deal with them? Which would I check first? Do I need to set some priority, so one overrules (if both are set) the other?

Example:

This example shows my dilemmas:

  • Which one has the higher priority? How can I avoid deciding?
  • It’s really long code just for deciding what to do with 2 vars.

Code:

if ( $nextpagelink && $next_text ) 
{
    // CASE 1: both are set
    // which one should I take now?
}
elseif ( $nextpagelink )
{
    // CASE 2: only one is set - should this one override the next one?
    // set something
}
elseif ( $next_text )
{
    // CASE 3: only one is set - should this be overridden by the previous one?
    // set
}
else
{
    // CASE 4: none is set - apply default
    // set default
}

Related posts

Leave a Reply

2 comments

  1. wp_link_pages() and paginate_links() work in different contexts: The first on a single paginated page, the latter on an archive. So is_archive() should be your criterion to decide which text you use, or better: which filter you use in apply_filters().

  2. Since both of them need to exist to get in to the first if statement, couldn’t you do something like this?

    if ( $nextpagelink && $next_text ) {
       // set both variables
    }else {
       // set default
    }
    

    or if you wanted to only work with one, regardless as to whether the other exists you could do this:

    if ( $nextpagelink || $next_text ) {
       if ( $nextpagelink ) {
           // set something
       }
    
       if ( $next_text ) {
           // set
       }
    }else {
       // set default
    }