Skip to content

Instantly share code, notes, and snippets.

@mgratch
Last active September 25, 2015 02:27
Show Gist options
  • Save mgratch/e7a3465653ef56527f37 to your computer and use it in GitHub Desktop.
Save mgratch/e7a3465653ef56527f37 to your computer and use it in GitHub Desktop.
Create a page, load a template file from plugin, pass input variables from one template to another via query_var
<?php
/**
* The template for displaying search results pages.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
get_header(); ?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
/**
* Here I am getting the zip code from the input on test-page.php
*/
$zip = get_query_var( 'dealer-search-form', 1 );
var_dump($zip);
?>
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyfifteen' ), get_search_query() ); ?></h1>
</header><!-- .page-header -->
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<?php
/*
* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called content-search.php and that will be used instead.
*/
get_template_part( 'content', 'search' );
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );
endif;
?>
</main><!-- .site-main -->
</section><!-- .content-area -->
<?php get_footer(); ?>
<?php
/**
* The template for displaying pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other "pages" on your WordPress site will use a different template.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<form action="<?php echo esc_url( home_url("/custom-search/")); ?>" id="searchform-dealer" method="POST">
<div>
<input type="text" id="dealer-search-form" name="dealer-search-form" value="" placeholder='Enter zip code...' />
<input type="submit" value="GO!" id="searchsubmit" class="button" />
</div>
</form>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
<?php
/*
Plugin Name: unique-plugin
Description: this is a description!
*/
/* include custom meta boxes as needed */
define( 'MI_PATH', plugin_dir_path(__FILE__) );
define('MI_URL', plugin_dir_url(__FILE__));
function create_pages(){
$user_id = get_current_user_id();
if (!post_exists('Custom Search')){
$defaults = array(
'post_title' => 'Custom Search',
'post_content' => '',
'post_name' => 'custom-search',
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => $user_id,
'ping_status' => 'closed',
'post_parent' => 0,
'post_excerpt' => ''
);
$post_id = wp_insert_post( $defaults );
}
if (!post_exists('Test Page')){
$defaults = array(
'post_title' => 'Test Page',
'post_content' => '',
'post_name' => 'test-page',
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => $user_id,
'ping_status' => 'closed',
'post_parent' => 0,
'post_excerpt' => ''
);
$post_id = wp_insert_post( $defaults );
}
}
add_action('admin_init','create_pages');
function include_template_files() {
global $wp;
if (is_page('custom-search')) {
$templatefilename = 'custom-search.php';
$template = MI_PATH . '/theme_files/' . $templatefilename;
include($template);
exit;
}
if (is_page('test-page')) {
$templatefilename = 'test-page.php';
$template = MI_PATH . '/theme_files/' . $templatefilename;
include($template);
exit;
}
}
add_action('template_redirect', 'include_template_files');
function add_referring_pic_data_query( $vars ){
$vars[] = 'dealer-search-form';
return $vars;
}
add_filter( 'query_vars', 'add_referring_pic_data_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment