Custom template for wordpress plugin

I am looking for a solution to add a custom template for a shortcode plugin. But I cannot do it successfully.

I created a template folder in my plugins folder and placed a custom template in it. I want to show this pattern by putting a shortcode. For this I wrote the following piece of code.

function wp_parse_login()
{
    add_action('template_redirect', 'my_template');
    function my_template()
    {
        include ('template/login.php');
        exit;
    }
}
add_shortcode('parse_login_page','wp_parse_login');

      

but it doesn't work. I have included this file in my main plugin file. I think I'm leaving the hooks.

+3


source to share


2 answers


Here's an example from the WordPress Codex



function wp_parse_login() {
    ob_start();
    include ('template/login.php');
    return ob_get_clean();
}
add_shortcode('parse_login_page','wp_parse_login');

      

+2


source


First, you set the page template to DB. For example:

$table_post_meta = $wpdb->prefix.'postmeta';

$meta_data = array(
    'post_id'       =>  $post_id, (Get dynamically post id of a page)
    'meta_key'      =>  '_wp_page_template',
    'meta_value'    =>  'template/login.php' (Give the file path)
);
$wpdb->insert($table_post_meta,$meta_data) or die(mysql_error());

      

You write in your code on your template page:



function wp_parse_login() {
    ob_start();
    include ('template/login.php');
    return ob_get_clean();
}
add_shortcode('parse_login_page','wp_parse_login');

      

I hope you find your solution.

0


source







All Articles