In my wordpress plugin I have a function which has a page id:
public function create_view_for_pdf() {
$page_id = $_POST['page_id'];
$this->template_shortcode('template.php');
}
here, "template_shortcode" function includes a template located in a folder in the plugin directory.
private function KendoPdf_template_shortcode($template_name) {
return include '/template/dir' . $template_name;
}
In the template file, I want to pass the page id so that I can print content there. How can I do that?
Note: Since I am just including the template file, I thought I will get the $page_id variable there normally. But it did not work. I needed the page id in template file because the content will be different than the actual page. That page also has ACF fields. I am basically creating a new template for pdf export. That's why I cannot use all the content of that page.
Please correct me if I am wrong but why would you pass the the page_id to the template file if it is within $_POST? Just access the variable using $_POST['page_id']; within your template file.
Instead of including your template file you could also read it into a string with file_get_contents(); and do your desired replacements before you return it.
Another possibility would be a global variable which is already set: global $post;
And last but not least you could use output buffering with ob_start(); (and consecutive functions).
You see: A lot of ways to solve this.
Related
I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.
I'm new to wordpress. Is it possible to define a variable inside the pages-admin area?
I got an if-else statement in my template. It creates a text-output. But i don't want to define this text hard coded inside the template-file. I use polylang-plugin for multi-language support. So I'd like to set up a page for each language, which should define its own $msg text.
Otherwise I'd need to check the languages inside the template to define the correct message in here - or use an extra template for each language. I don't beleave, that this is the only solution.
I'm looking for something like this ->
in template.php:
if (empty($statement)) {
echo $msg1;
}
in admin-pages something like:
$msg1 = "no entries found";
how do I define vars outside the template-file?
You can define variables within your theme functions file (functions.php) or a custom plugin. You can take advantage of these via hooks and filters within WordPress core and your theme if it is supported. Here's an example using functions.php.
add_filter('the_content', 'my_custom_var');
function my_custom_var() {
$my_var = 'something different'
echo $my_var;
}
This will insert the echoed variable into any template file that uses the_content. Although very basic, this functionality will give you plenty of flexibility.
i have to header like.
header-first.php
header-second.php
i create two template like first and second. when the first template is load then i want to load header-first.php and this is working. then second like first.
i have problem with when i load second template then the header-second.php is load is working but not when i go to any page then i want to load header-second.php but not it load header.php.
when i load a second template then the header-second.php is loaded working. now i going any page and view post then the header.php file is load i want to load header-second.php file. please help how can i set globally.
i try to set globally in function.php like.
global $header;
if(is_page_template('page-second.php')){
header = 'second';
}else if(is_page_template('page-first.php')){
header = 'first';
}
it working it return only template page is load.
when i try to load any other like about page it return '' value. it not return any value.
i want to second whenever the first template is not visit.
thank you.
Each template file such as page.php, single.php, archive.php, etc, will have get_header() at the top of it.
This function loads the header (header.php).
If you pass in a string as the first argument it will try to load header-{argument}.php and if not found it will load header.php.
Anywhere you want to load header-second.php you need to change get_header(); to get_header( 'second' );
There is an action that fires when this function runs but there's no filter so you can't override it globally like you're attempting to do. Instead you need to update the template files individually with the function I showed you above.
To summarise change:
<?php get_header(); ?>
To:
<?php get_header( 'second' ); ?>
Further reading: http://codex.wordpress.org/Function_Reference/get_header
This question is regarding wordpress.
I have two variables $random_n and $adcount declared in each of index.php , archieve.php, search.php and single.php files at the very first line.
The variables are declared as follows:
<?php $random_n = randomNumber();?>
<?php $adcount = 1; ?>
randomNumber() is a function inside function.php.
If I move those variables to header.php and remove them from all these files, it stops working. :/ The variables can't be found in index.php, archieve.php, search.php, single.php if they are placed in header.php
I've also tried putting this code inside a new file and including that file to index.php, archieve.php, search.php, and single.php, but it hadn't helped as well.
If the variable is somehow related to your blog configuration, you should use the wordpress option API to build this (see here , search for option).
If your variable is random (your function name hints that) a way to work around this would be using session variables to store those values.
A good tutorial on how to use session variables in wordpress can be found here:
http://www.myguysolutions.com/2010/04/14/how-to-enable-the-use-of-sessions-on-your-wordpress-blog/
Type in var_dump($random_n) into the beginning of index.php and post the output, please.
One possible reason is that your index, search and so on are included inside the function that does not share the scope with header.php, so your vars are just outside of the scope. It might help if you declare them global both in header.php and your working files. But that's not a good idea.
Better solution would be to introduce some Registry object (Registry pattern) and use it.
I'm trying to theme a view in Drupal 7. I have the view theme set-up and it is working fine. The issue that I have is that I can't see a way to override the $content variable passed into the render() function.
Is there a pre/process hook that I should be using or should it be done in a .tpl file using the $node variable?
Currently I'm looking at the $node variable but the image attached to the content has a url of public://field/image/imagefield_JmDqqm.jpg and I've not been able to find a function (so far) that will parse that url into the correct url to view the image on the page.
Thanks for any and all help.
The file_create_url function is what you're after