Can't see php-content from custom template in WordPress - php

I'm trying to create a custom template for a small wordpress site, the template should display some data from the database.
But I can't get PHP to output anything on the pages, I've written a few echo's just to get something but can't see anything.
I can, however, add the page through "Pages" in WordPress, that works just fine and displays an empty page (or with text if I write something in the text box)
To set up the template I created a child template of my original template, the CSS file in the child template works just fine.
I added the custom page called "petitionlist.php" in the root of my child-theme, the code I added for testing is as following
<?php
/*
Template Name: petitionlist
*/
get_header();
echo "test test test";
$test = "testtetsttest";
echo "<h1> this is a test</h1>".$test; ?>
<h1>Hello wordl!</h1>
<?php get_footer(); ?>
Attached is a screenshot of my folder-structure, the petitionlist.php file in wechange-child is the file I'm working on.

Related

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 shortcodes not working

Everytime i try to add a shortcode into my Wordpress site it ends up merely displaying the code i add to the actual page. I've been pasting the code into the "text" area of the content section but still doesnt work.
Even when I paste inside of the header.php file I recieve the same result. For example.
I recently copy pasted the following code in header.php (as instructed by the plugin i downloaded)
<div class="headerslider"> <?php echo do_shortcode('[sp_responsiveslider limit="-1"]'); ?></div>
When I save a reload, I just get [sp_responsiveslider limit="-1"] appear in my website. This happens with all shortcodes i do.
Please help
It seems like the shortcode sp_responsiveslider isn't actually defined. Are you sure the plugin is installed correctly? If the plugin is installed correctly you can test to see if shortcodes are working on your WordPress installation. Paste the following in your functions.php file (add <?php to the first line if it's not already in the file):
function test_shortcodes()
{
return 'Shortcodes are working!';
}
add_shortcode('test_shortcodes', 'test_shortcodes');
This will create a new shortcode in WordPress. To test if shortcodes are working put the text [test_shortcodes] within a post and view the page with the post. When you load the page the shortcode tag should be replaced with "Shortcodes are working!". If this works, there is an issue with the plugin and it's shortcode.
If the test text is not displayed there might be a problem with your WordPress installation.

Keeping WordPress theme and layout when opening custom php file

I'm trying to incorporate some custom made php files in a WordPress site.
In these files made links to other files.
<?
echo "" $row['Name'] . "<br> ";
?>
This works fine with an php-execute plugin for WordPress. The only thing is that the show.php opens without the WordPress theme and layout.
How can I make a link so that a new php file will open in the same (side)bar/frame with the same layout as the WordPress page?
you need to create a custom template for that
The syntax is something like this:
<?php
/*
Template Name: My Custom Page
*/
get_header();
?>
Here your HTML coding goes
<?php get_footer(); ?>
This will give you the look and feel you are looking for!!
Found my own work around
Create a page in WordPress (page_id=x) and include an exec-php plugin.
Include this code on the page.
<?
$page_include =$_GET["page_include"];
include $page_include;
?>
If you want to show a custom page in the wordpress them make the following link:
<?
echo " link "
?>

Include wordpress theme in a custom php page

I need to include a custom PHP page in Wordpress.
So what I need to do is just to show this custom php page using the Wordpress theme installed on that Wordpress.
Does not mind which theme is up, the custom php page will have to be shown under any theme is installed in that moment.
How do I do it in Wordpress?
I am new to Wordpress development.
Thanks
Creating a custom php page that will be able to be viewed in any theme (and have the theme applied) would be considerably difficult.
Each wordpress page calls specific theme functions of that particular theme, as well as referencing files of that theme to generate header, footer, css files, javascript files, etc.. Your custom page would need to plan for all of these contingencies, for each possible theme used.
Here's a alternative solution: inject PHP code directly into a standard wordpress page via this plugin http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Meaning: you make a normal wordpress page, but are able to add php to it. When this page is rendered, the proper page template is used, and all the theme references are taken care of for you.
You could do this easily with a page template. WordPress allows you to create page templates which can be assigned to a page via the 'Page Attributes' panel within the page editor. These templates are php files inside your theme directory which begin with some code like (see this page in The Codex for more info):
<?php
/*
Template name: Custom PHP Page
*/
?>
<?php // begin custom PHP page ?>
Typically a template is a variation on the regular theme files (such as page.php) and would call the get_header() and get_footer() functions and have an instance of the loop. However if you simply want to use a custom PHP page, then all you need to do is create the file you want inside the current theme directory and add the above code at the very top of the file.
To output the custom PHP page on your site, you would need to add a new page via the admin area and then assign your new page template to this page.
Alternatively, if you want to include a custom PHP page inside an existing theme file, you use the code:
<?php include(TEMPLATEPATH . '/includes/file.php'); ?>
in this case your custom PHP file would be located inside a directory called 'includes' within your current theme directory.
Tim.
It's not that difficult. Here's what you need:
Once you include the main wordpress blog header, the entire armamentarium of wordpress functions is available to you, which allows you to get the active theme's directory. Once you get that, just include the header and the footer of the theme.
// If title is not displayed before loading the header, Wordpress displays "Page not found" as the title
echo "<head>
<title>Your page title</title>
</head>";
// Include the Main Wordpress blog header
include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";
//Now, you need to get the active theme's folder, and get a relative path to that folder
$homeurl=home_url();
$ddir= get_bloginfo( 'template_directory');
$current_theme_relative_path=substr_replace($ddir, "", 0, strlen($homeurl));
//echo "<br/>The relative path to the currently active theme is ".$current_theme_relative_path;
//Once you have the path, include the header and footer, adding your custom php code in between.
// Include the specific theme header you need
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/header.php";
// Your custom PHP code STARTS here
// Add anything you want to display to the user
echo "
<h2>
Your form has been submitted
</h2>";
// END of custom code
?>
<?php
}
// Now end with the theme's footer
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/footer.php";
?>
Was very helpfull (even if dated of 2011-13)
Also, as a thank you, i'm sharing the version i made
it's usefull if your wordpress folder is not located at ROOT
PasBin Link - wordpress custom php page
just change the value of $wplocalpath in :
// Wordpress path (if wordpress is not located at ROOT
// $wplocalpath="/Wordpress1";

Categories