• Resolved davidedw

    (@davidedw)


    I’ve written a new template to show the top 10 rated books (“orderby=rating&order=desc&num=1-“).

    I’m trying to figure out how to refer to this new file, favourites.php, which is in my template/now-reading-reloaded directory.

    I can see references to library_url() in template-functions.php and various calls in the url.php file, but I’m still unclear of the best way. Any ideas?

Viewing 1 replies (of 1 total)
  • Thread Starter davidedw

    (@davidedw)

    I figured it out. The Now Reading (Reloaded) functions require a URL string of something like; blog/index.php?now_reading_<blahtype>=<blahvalue>. Where blahtype is something like library, tag, author etc. and blahvalue relates to the type (e.g now_reading_library=1, now_reading_tag=Doctor).

    So to add a new template file you need to extend everything that plays with the URL and query strings…. For example, I decided to add a “now_reading_page=favourites.php” so I could add multiple pages with one set of plugin changes.

    The files I had to modify were:
    1. url.php: this module processes the URL’s that use the mod_rewrite function. I had to modify the
    1a. the nr_query_vars function to add a new variable (now_reading_page)…

    function nr_query_vars( $vars ) {
        $vars[] = 'now_reading_library';
        $vars[] = 'now_reading_id';
        $vars[] = 'now_reading_tag';
        $vars[] = 'now_reading_page';
        $vars[] = 'now_reading_search';
        $vars[] = 'now_reading_title';
        $vars[] = 'now_reading_author';
        $vars[] = 'now_reading_reader'; //in order to filter books by reader
        return $vars;
    }

    1b. the nr_mod_rewrite function to add a new mod_rewrite rule for URL’s of the format library/page/favourites.php…

    function nr_mod_rewrite( $rules ) {
        $options = get_option('nowReadingOptions');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . '([0-9]+)/?$', 'index.php?now_reading_id=$matches[1]', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . 'tag/([^/]+)/?$', 'index.php?now_reading_tag=$matches[1]', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . 'page/([^/]+)/?$', 'index.php?now_reading_page=$matches[1]', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . 'search/?$', 'index.php?now_reading_search=true', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . 'reader/([^/]+)/?$', 'index.php?now_reading_library=1&now_reading_reader=$matches[1]', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . '([^/]+)/([^/]+)/?$', 'index.php?now_reading_author=$matches[1]&now_reading_title=$matches[2]', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . '([^/]+)/?$', 'index.php?now_reading_author=$matches[1]', 'top');
        add_rewrite_rule(preg_quote($options['permalinkBase']) . '?$', 'index.php?now_reading_library=1', 'top');
    }

    1c. The is_now_reading_page function to make the new query string register as a now_reading page…

    function is_now_reading_page() {
        global $wp;
        $wp->parse_request();
    
        return (
        get_query_var('now_reading_library') ||
            get_query_var('now_reading_search')  ||
            get_query_var('now_reading_id')      ||
            get_query_var('now_reading_tag')     ||
            get_query_var('now_reading_page')    ||
            get_query_var('now_reading_title')   ||
            get_query_var('now_reading_author')
        );
    }

    2. now-reading.php: the main file in the library_init() function to add a new check:

    if ( get_query_var('now_reading_page') ) {
        // get page name from query string:
            $nrr_page = get_query_var('now_reading_page');
    
            $load = nr_load_template($nrr_page);
            if ( is_wp_error($load) )
                echo $load->get_error_message();
    
            die;
        }

    3. I also had to create a new function in template-functions.php to return either a normal or mod_rewrite URL…

    /**
     * Returns a URL to the permalink for a given (custom) page.
     * @param string $page Page name (e.g. custom.php) to create URL for.
     * @param bool $echo Whether or not to echo the results.
     */
    function library_page_url( $page, $echo = true ) {
        $options = get_option('nowReadingOptions');
    
        if ( $options['useModRewrite'] )
            $url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/page/" . urlencode($page);
        else
            $url = get_option('home') . '/index.php?now_reading_page=' . urlencode($page);
    
        $url = apply_filters('library_page_url', $url);
    
        if ( $echo )
            echo $url;
        return $url;
    }

    I have no idea what the apply_filters function is doing.

Viewing 1 replies (of 1 total)
  • The topic ‘[Plugin: Now Reading Reloaded] How to add a new template file’ is closed to new replies.