Example #1
0
/**
 * Get page URL by template
 *
 * @since 1.7.1
 * @param string|array $templates Template or array of templates (first match used)
 * @return int Page URL, if page was found; otherwise empty
 */
function ctfw_get_page_url_by_template($templates)
{
    $url = '';
    $page = ctfw_get_page_by_template($templates);
    if ($page) {
        $url = get_permalink($page);
    }
    return apply_filters('ctfw_get_page_url_by_template', $url, $templates);
}
/**
 * Check if homepage set for static front before import
 *
 * Set a global if page using homepage template does not exist before import.
 * This helps determine after import if the homepage was imported.
 *
 * add_theme_support( 'ctfw-import-set-static-front' );
 *
 * @since 0.9.3
 * @global $ctfw_import_no_homepage_before
 */
function ctfw_import_check_static_front()
{
    global $ctfw_import_no_homepage_before;
    // Default
    $ctfw_import_no_homepage_before = true;
    // Theme supports this?
    $support = get_theme_support('ctfw-import-set-static-front');
    if (!empty($support[0])) {
        // Get homepage template
        $homepage_tpl = $support[0];
        // Check if page using that template exists
        $homepage_page = ctfw_get_page_by_template($homepage_tpl);
        if (!$homepage_page) {
            // If not, we'll want to set homepage after import
            $ctfw_import_no_homepage_before = true;
        }
    }
}
/**
 * Redirect post type archives to pages
 *
 * Use add_theme_support( 'ctfw-archive-redirection' ) to redirect post type archives to pages using specific page templates.
 * Post types and page templates from ctfw_content_types() are used to automate this (theme must filter page templates in).
 *
 * Page template should output same loop but with with title, featured image, etc. for nicer presentation and to avoid duplicate content.
 * This is done only for non-date archive. Feeds are unaffected.
 *
 * @since 0.9
 */
function ctfw_redirect_archives_to_pages()
{
    // Theme supports this?
    if (!current_theme_supports('ctfw-archive-redirection')) {
        return false;
    }
    // Run only on post type archive, but not date archive or feed
    if (!is_post_type_archive() || is_year() || is_month() || is_day() || is_feed()) {
        return false;
    }
    // Get content types
    $content_types = ctfw_content_types();
    // Loop content types
    foreach ($content_types as $content_type => $content_type_data) {
        // Get templates for content type
        // The first will be used if a page exists; otherwise the second, etc.
        $page_templates = !empty($content_type_data['page_templates']) ? $content_type_data['page_templates'] : array();
        // Have at least one page template
        if ($page_templates) {
            // Get post type(s) for content type (probably just one)
            $post_types = $content_type_data['post_types'];
            // Have post types
            if (!empty($post_types)) {
                // Loop post types
                foreach ($post_types as $post_type) {
                    // Have post type
                    if (!empty($post_type)) {
                        // Only if archive is for specific post type
                        if (is_post_type_archive($post_type)) {
                            // Loop each template in order of priority and redirect to first one that has page
                            foreach ($page_templates as $page_template) {
                                // Check if a page is the template
                                if ($redirect_page = ctfw_get_page_by_template($page_template)) {
                                    // Found a page?
                                    if (!empty($redirect_page->ID)) {
                                        // Get page data
                                        $post_type_obj = get_post_type_object($post_type);
                                        // Don't redirect if URL is the same (post type and page have same slug); prevent infinite loop
                                        if ($redirect_page->post_name != $post_type_obj->rewrite['slug']) {
                                            // Get URL
                                            $page_url = get_permalink($redirect_page->ID);
                                            // Go!
                                            wp_redirect($page_url, 301);
                                            exit;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}