Load custom theme in specific page / post in wordpress plugin (PHP) - php

I am developing a plugin and I want to show my custom HTML CSS codes on a page or post with a shortcode.
I have this template:
<?php
/**
* Template Name: Raw page
*/
get_content();
?>
and this code on my class:
add_shortcode('my-shortcode', 'callback');
...
public function callback(){
include('view/form.php');
}
now how can I load this template from my function?

Use wordpress function get_template_part.
Check ref here - https://developer.wordpress.org/reference/functions/get_template_part/
And, another suggestion, does your call back/ short code really required ? You can simply write your logic in your primary template file (where you wrote as raw file).
Then put your logic/conditions there and can include different template parts as per your need.

Related

new template in wordpress is not displayed under page attributes -> template

i want to integrate several layouts into my theme. so far I have solved it via page.php and page-blue.php because I saw it that way in tutorials. unfortunately the other template is not displayed under the page attributes. i don't see the error. even if i choose a different theme the attributes do not appear. what am I doing wrong?
it should look like this
this is how it looks
this is the page.php
this is the page-blue.php
Change the name of the template, page is reserved word, also create brand-new file with file name other than page.php, which is also reserved file name.
And, for god's sake, name your template files in a way they mean something, self-explanatory.
Example for dummies:
<?php
/*
* Template Name: Some Template
*/
And the file name of the template should be some-template.php inside your theme's folder!
Read more
pls Use This code
<?php
/*
* Template Name: page-blue
* Template Post Type: post
*/
get_header();
?>
instead of this
<?php
/*
* Template Name: page-blue
*/
get_header();
?>

Wordpress Adding Custom HTTP Header To One Template File

I need to add a header Access-Control-Allow-Origin: * into one of my template files. How would I do this I would like it to be for just this specific template so that its not site wide,
I think you're looking for is_page_template();
<?php
if(is_page_template('your-page-template.php')) { //Change this to the path to your wordpress template
//Add your code here...
} ?>
you can create a custom template page and call the custom Header on this template.
To create a page and apply a page template
use this code to create page template
First you need to have a single page. There are several ways to do it, one is to create a page in wordpress, then use the ID and create a file page-ID.php in the theme folder. Other way is to create a template file which you can select from the side menu in the wordpress editor.
In that page, you can add the php header and the code you want to show.

Wordpress add .php file manually

I have a .php file which uses some WP functions e.g. get_stylesheet_directory_uri(), query_posts, etc... and $wpdb of course.
File it self return JSON data so it is not intended for viewing on its own.
I'm not looking for template, or clicking around in WP dashboard, I just want to know where is the good place in WP file structure to put the .php file and expect WP functions and object to be available.
And also what do I need to include at the top?
You can create custom template in theme root directory or in child theme (child theme is important when you are creating custom template).
template should contain template name, header and footer function.
eg. custom-tpl.php
<?php
Template Name: My Custom Temlate
get_header();
//your other functions and content goes here
get_footer();
after creating template it will appear in your edit page template section under drop down. select custom template for your page and view page you will get your output
You can create a custom page template.
Inside the page template:
<?php
global $wpdb;
[here you will write your code]
?>
On the front pages where you want the json output, use this url
http://domainname.com/wp-content/themes/themename/custompagetemplatename.php
Use your domain name (domainname), theme name (themename) and theme template file name (custompagetemplatename) in place of example names in the url. This url will return you the output for your code.
About this comment in your template:
<?php
/* Template Name: Full Width Page */
?>
You do not need a template name in the header of your file unless you want to use this as a template for pages in wordpress.

Visual composer Masonry grid doesn't load

I'm using [insert_php] plugin in Wordpress and I have inserted php function in it.If I set the block with the function before the the masonry grid it doesn't load.
If I move the block with the function after the grid it works well.
In console log is appearing an alert:
Syntax error, unrecognized expression: {'status':'Nothing found'}
How can I sole that?
Grid before php block
Block php before the grid
What you need to do is create a page template. This page template can be used to create PHP for your page to load, and you can even use
<?php echo do_shortcode('[shortcode]'); ?>
To create a template just copy your page.php into a new file called tpl_test.php.
Then make sure to have this in the very first line:
`<?php
/*
Template Name: Test Template
*/
?>`
Then, a small window will show up on the back-end of WordPress when you edit the page. In the right hand column you need to select the page template for the page you just created, and update the page. then, any code you put in the custom Page Template will display correctly on your WordPress site. Also, make sure <?php the_content() ?> is below your grid you are trying to output.

Wordpress function access through url

Is there any option to access a function inside function.php through a url?
Like joomla has this.
Any idea?
Use ajax functionality of wordpress:
HERE
You can use nopriv actions for that.
wp-admin/admin-ajax.php is the url for the ajax page of wordpress.
Or either you can make a template page which gives only content and create a shortcode
and add to a specific page.
functions.php is part of the core of your theme and is automatically included.
to access your function, simply use it in your theme file, as such:
<?php myfunc(); ?>

Categories