0

so I was asked to create url rewrites for one of the websites we're working on, but the thing is the test rewrite I did, just to try it out, is not really working.

here's the code I put in the functions.php file of the theme, to do url rewrites;

add_action( 'init', 'initialize_my_theme' );
function initialize_my_theme() {
    register_nav_menu('primary','MainMenu');
    add_filter( 'query_vars', 'campedia_set_query_vars' );
}

function campedia_set_query_vars( $vars ) {
    //array_push( $vars, 'learner_id' );
    return $vars;
}

function campedia_activate() {
    // register taxonomies/post types here
    flush_rewrite_rules();
    add_rewrite_rule('^nutrition/.+','view-profile/#','top');
}
register_activation_hook( __FILE__, 'campedia_activate' );

function campedia_deactivate() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'campedia_deactivate' );

now I know you can't just take the examples from the wordpress site and expect everything to work - [cos their examples almost never work for the complicated stuff] - but I got practically all of this code from their website and it's not working, this is the third thing I tried and each time I'm just redirected to the home page. why?!

1
  • 2
    Please use proper capitalization, real English words (cos -> because) and punctuation. At least this is no chat, but questions and answers serve a purpose a reference for later readers as well. Thanks.
    – kaiser
    Commented Oct 22, 2013 at 12:24

1 Answer 1

1

All the activation/deactivation/uninstall hooks only trigger for plugins.

Your best bet for a theme is the switch_theme hook, which has one argument: $new_theme. More info in that answer by @prettyboymp.

1
  • 1
    And URL rewrites is Plugin Functionality, and thus belongs in a Plugin rather than in a Theme. As an added bonus, when properly moving URL rewrites to a Plugin, activation/deactivation hooks become available. Commented Oct 22, 2013 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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