DotDragnet
May 24, 2012, 07:40:20 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: custom permalinks in WordPress for own script  (Read 418 times)
SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« on: October 10, 2011, 03:57:19 PM »

I havea  page template with a call to an external api. I want to be able to use permalinks on this instad of having ?jid=1 etc
I assume it is possible as other plugins have done this but anyone had any experience can point me in the right direction on how this would work?

Also is it possible to paginate these as well, would i need to create my own pagination script or is there a way to makie use of wp_pagenav for example?
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #1 on: October 10, 2011, 08:22:25 PM »

For the permalinks you want to use say

/uniqueslugorpageslug/jid/1/

then in your plugin/function file use

Code:
// do some rewrites for the Job Vacancy pages
add_action('init', 'fh_flush_rewrite_rules');

function fh_flush_rewrite_rules()
{
   global $wp_rewrite;
   
   add_filter('query_vars', 'fh_add_queryvars');
   $wp_rewrite->flush_rules();
}

function fh_add_queryvars( $query_vars ){

$query_vars[] = 'jid';
return $query_vars;
}

add_action('generate_rewrite_rules', 'fh_add_rewrite_rules');

function fh_add_rewrite_rules($wp_rewrite) {
$new_rules = array(
'uniqueslugorpagename/jid/([0-9]+)/?$' => '?jid='.$wp_rewrite->preg_index(2)
);

$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

Then in your page template that gets the value of jid you can't use $_GET as that won't work you need to use

Code:
if (get_query_var('jid')) :
$jid = get_query_var('jid');
else :
// do something if jid isn't set
endif;

for pagination I don't know about page navi but you could just tag /page/2/ on the end and add that to the rewrite rule to pass through the paged value (I think it's paged, you may need to check what the variable should be called in the URL).
Logged

SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« Reply #2 on: October 11, 2011, 09:00:18 AM »

thanks Sarah. Only thing was the add action the name of the function doesnt reference anything should that be chanegd to be:

add_action('generate_rewrite_rules', 'fh_flush_rewrite_rules');

I tried it but i got an out of memory so not sure if this is correct?
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #3 on: October 11, 2011, 03:37:25 PM »

I'm lost? There are two add actions. The first flushes the rewrite rules, the second creates the new ones during generate rewrite rules. You did scroll the code right?
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!