Embedding Wordpress header and footer in Yii framework app - php

I'm building a website that uses Wordpress as the CMS and is built with Yii framework. All the business logic works fine. Most visible pages are filled with content from Wordpress admin and only the user profile section is built with Yii.
My problem is that I would like to reuse the layout of the wordpress pages in the Yii app. Initially I tried to do it in a blank PHP file and everything worked as expected with this code:
<?php
require( '/var/www/wordpress/wp-load.php' );
get_header();
?>
<div>
some content here
</div>
<?php
get_footer();
?>
The problem is that when I make a new layout in Yii so that I can reuse the header and footer it crashes and displays blank page. I tried both with require('/var/www/wordpress/wp-blog-header.php'); and require( '/var/www/wordpress/wp-load.php' );.
This is the code I use to load wordpress functionality in Yii but unfortunately it does not work.
<?php /* #var $this Controller */
spl_autoload_unregister(array('YiiBase','autoload'));
define('WP_USE_THEMES', true);
require('/var/www/wordpress/wp-blog-header.php');
//require( '/var/www/wordpress/wp-load.php' );
get_header();
spl_autoload_register(array('YiiBase', 'autoload'));
?>
Any help would be much appreciated. Ideally I would like to use get_header() and get_footer() wordpress functions in Yii layouts/views.
Thanks
UPDATE
I checked Apache logs and it seems that the following error is logged when I try to open the page in my browser: PHP Fatal error: Call to a member function get() on a non-object in /var/www/wordpress/wp-includes/query.php on line 27
Also when I comment out the get_header(); row the page opens without any errors (and without any styling too). So something prevents get_header() to work property in Yii.

I ended up adding require( '/var/www/wordpress/wp-load.php' ); in the Yii's index.php file just before the initialization code. Then I was able to use all WordPress functions in my project. This helped me reuse the header and footer layout from WordPress in Yii using the get_header() and get_footer() functions.

Related

Need to create a custom php page for a wordpress site having no theme folder

I am new to WP, so please excuse if I ask anything silly.
I have WordPress site which have that created using builder and doesn't use page template of theme folder, everything is built on page builder. I also have other project that I've created on core php.
Is there a way to integrate Core PHP website with Wordpress website?
Creating a Page Template
Creating a page template is extremely easy. Create any new file in your theme (wp-content/themes/your-theme/my-custom-page.php) and start the file with a comment block like so:
<?php
/*
Template Name: My Awesome Custom Page
*/
get_header();
?>
/*
Code Here
*/
<?php
get_footer();
?>
You need paste php code inside this template file. When you submit for then redirection action will be page with custom template page.
Reference :
https://premium.wpmudev.org/blog/creating-custom-page-templates-in-wordpress/
Video :
https://youtu.be/1ZdHI8HuboA

WordPress Custom Template - Divi module shortcode displays as text

I have written a head navigation using the full-width code module in Divi. It is in my Divi library as a global module and works perfectly with most of my pages, but not the ones that are using my custom templates.
The way I am displaying the module is by using it's shortcode at the bottom of my child theme's header.php right before the closing header tag:
<?php echo do_shortcode('[showmodule id="XXXX"]'); ?>
where XXXX is the actual id. But on the pages using custom templates the module displays as plain text like so:
'[et_pb_section global_module="my modules id"][/et_pb_section]'
I have also tried changing the code to:
echo apply_filters('the_content','[showmodule id="XXXX"]');
however, the same issue occurs.
Any Ideas?
I found a good method for this in a thread in the Divi Support Forum, which this dude also explains in a video:
https://www.youtube.com/watch?v=PJqcfz5NyZs
Once you get the global Module number from the URL when viewing it in the Divi Library, the shortcode syntax to load the global module in php is:
<?php echo do_shortcode('[et_pb_section global_module="###"][/et_pb_section]'); ?>
One drawback is that loading a module through a php template file does not allow it to be editable through the Visual Builder.
So, I made a modification to this so I could have an easier place to edit the global footer by adding it to my site's homepage Divi layout, then adding this snippet in my footer.php template file to load on the rest of the pages:
<?php if ( !is_front_page() ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="2310"][/et_pb_section]'); ?>
<?php endif; ?>
This way I can easily have a client use the Visual Builder on the homepage to edit the global footer, instead of having to find it in the Divi Library, which they would likely forget and takes extra click to get there.
Please try adding this code. It will solve your problem
<?php echo do_shortcode('[showmodule id="my modules id"]'); ?>

Get page.php using php code

I am working with wordpress.
I see that in my index.php there is a code called <?php get_footer(); ?> ..and I get it, it's simple. This code will pull footer.php.
It seems to me that the get_footer() is built in to wordpress that pulls anything that is named footer.php.
I have created my own page called page.php.
I want to be able to 'get' this page and show in my php code enabled 'sidebar widget'.
I have tried to paste this code, and I am more that certain that its wrong:
<?php
echo file_get_contents("side.php");
?>
What code would I need if I want my custom page called page.php to be shown?
The WordPress way to load a template file is get_template_part():
get_template_part( 'page' );
// loads page.php from the theme directory.
Note that page.php is a special filename in WordPress themes, this file is loaded as a template if a page is displayed. You should give your file a different name if you want to prevent this.
Details are in the already mentioned template-hierarchy.png
Edit:
After rereading your question: If your page.php is not from a template, but in a folder of a plugin you are developing as a sidebar widget, the also already mentioned include('page.php'); would be the way to go.
page.php is a special page of wordpress. See this diagram.
https://developer.wordpress.org/files/2014/10/template-hierarchy.png.
The wordpress way is to create a own template.
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
try the following
include ('side.php');
OR
require ('side.php');
you may also use include_once / require_once
Please use require_once or include in php file like below.
require_once('side.php');
or
include ('side.php');
try this,
require( dirname( __FILE__ ) . '/page.php' );
if your page is in same level where is your parent page.
Add this code in your php page
<?php include_once('filename.php'); ?>
There are so many options that you can chose:
You can use get_template_part():
From the Docs: WordPress now offers a function, get_template_part(),
that is part of the native API and is used specifically for reusing
sections - or templates - of code (except for the header, footer, and
sidebar) through your theme.
Other solution is require_once() or include_once().
But if i compare both function include_once() is faster than to require_once() for smaller application.
For better understanding about the require and include you can read this Question.

How to include PHP file in Wordpress that is outside the Wordpress Environment

I am using a custom CMS for a website that deals with 90% of the site's pages and content.
However, for the news/blog section of the site, I am using Wordpress.
I'm able to include the necessary Wordpress files to pull in posts to the main site, but cannot do the reverse.
I want to be able to call a php file into the header of Wordpress so I have access to the CMS's classes, primarily for navigation.
I've tried a search but can't seem to find an answer.
Additional Information:
Wordpress is installed in site.com/wordpress
The file I want to include is in site.com/includes
Clarification:
I don't want to create a custom template or include Wordpress functionality outside of Wordpress. I want to pull external classes from a CMS, which is a level above Wordpress, into Wordpress so that I can use it throughout the Wordpress installation.
You should go with custom page template.
Its an easy way to include php file in wordpress because in thisway you can easily include wordpress theme header and footer.
To create a custom page template.
You have to simply create new .php page and at start of page just include this code.
<?php
/*
Template Name: My custom page template name
*/
?>
// your code
To read in detail about Wordpress custom page template click here
I hope it will help you.
Method 1 :
<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
Method 2 :
<?php
define('WP_USE_THEMES', false);
require('./wp-load.php');
?>
I managed to fix this by altering the include line to reference the folder above the Wordpress root.
So, the line became require_once "../path/to/file" instead of just require_once "/path/to/file"

Creating a custom page on wordpress and adding content to it

I want to create a php page on the website (CMS Wordpress), but I do not understand how to incorporate the template files, for example
<?php get_header(); ?>
And other content. I know that it is possible to create pages through the admin interface, but I do not fit... if you try to create a page of template files and then go for it the direct link:
http://local/wp-content/themes/mytheme/mypage.php
with pre-embedding the code
<?php get_header(); ?>
it will fail like:
Fatal error: Call to undefined function get_header()
What should I do?
How i can "say" this CMS that the page is important for me, and it performs some functionality? That is, in fact, get the full url to her and that did not give out errors…
You need to include wp-load.php in your script to use any of WP functions:
<?
require_once('../../../wp-load.php');
get_header(); ?>
try that. xD

Categories