Using Wordpress and Codeigniter in one website - php

Is it possible to use WordPress as the CMS but all of contents will be displayed using CodeIgniter?
What I want to know also if I can use the functions of WordPress by simply including this wp-blog-header.php in the CodeIgniter.
Do you think guys this idea will work?
Probably one of the function I really need is apply_filters().

I used the wordpress headers on a codeigniter site once, and there were a lot of conflicts with global functions like site_url that had to be manually resolved (by renaming those functions in the codeigniter core)
It was a mess, and I wouldn't recommend it if you can get away with it.
You might be better off querying the same database and building new functionality in codeigniter using the same data.
But if you still want to do it, you can do something like this:
// in codeigniter index.php
require_once( '../wordpress/wp-blog-header.php' );
Then (in your header view, for example):
if(function_exists('get_header'))
{
get_header(); // will echo the wordpress header
} else {
//html code for your header ?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<!-- etc... -->
<?php } ?>
And you should see the wordpress header, at least once you solve all the errors caused by including it. There's also a get_footer() function for the footer.
Good luck

Related

What is the best and most reliable way to call other templates into page.php in Wordpress?

My Wordpress website uses two push menus, both of which I initially had situated in the header.php file. It wasn't until I started needing to use a lot of PHP code in one of these menus that I decided to have them in separate files and call them into the page structure via PHP commands. Ive been wondering what would be the best way of going about this and would appreciate any advice on whether I'm doing it correctly. Below is the code from the simpler of the two menus – I've saved this as mobile-menu.php.
<div class="mobile-menu">
<nav class="main-menu" id="mobile">
<?php wp_nav_menu( array( 'theme_location' => 'header' ) ); ?>
</nav>
</div>
This isn't a particularly long list of code, but the other menu (shopping-basket.php)is a lot longer as it uses PHP from a plugin I've installed, and I don't want masses of code in my header.
I've then called both menus into my page.php using "include" commands, like I have with the header and footer – for example:
<?php get_header(); ?>
<?php include("mobile-menu.php"); ?>
<?php include("shopping-basket.php"); ?>
Is this recommended? It seems to work okay, but I've noticed that despite saving the files as PHP templates with a .php extension, they're showing up as HTML files when I have a look in my FTP account. Why would this be?
Any advice or info would be appreciated as I can't really find any concrete info on this online and have got to the stage that I'm at by searching through forums etc.
You would be best off using get_template_part() instead of include().
Load a template part into a template
https://developer.wordpress.org/reference/functions/get_template_part/
You should put the contents of mobile-menu.php into templates/menu-mobile.php (Note that I've reordered the name for least- to most- specific) and templates/shoppingbasket.php. Call them as so:
get_template_part('templates/menu','mobile');
get_template_part('templates/shoppingbasket');

Load layout once in Fat-Free Framework

I think this should have a pretty simple explanation but I'm still learning Fat-Free Framework (F3): How do I render only once the header and footer and switch out the content code for the selected route? I have this code:
$f3->route('GET /',
function($f3) {
$f3->set('content','views/welcome.htm');
$f3->set('page_head', 'Welcome');
}
);
And if I add this line:
echo View::instance()->render('layouts/header+footer.htm');
either after the f3->set calls in the route or after $f3->run(); at the end of the index.php file, the whole page refreshes on a route change. I can't call that echo line above before the route code without throwing an error in the content box.
Is there any way to disable the page refreshing? Is it being refreshed because my links are being interpreted as separate pages by the browser? Thanks for any help!
Your question is a little hazy. But I will try to answer in the way I understand.
First your index.php
$f3->route('GET /',
function($f3) {
$f3->set('content','views/welcome.htm');
$f3->set('page_head', 'Welcome');
echo Template::instance()->render('layouts/header+footer.htm');
}
);
$f3->run();
In your header+footer.htm
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>{{#page_head}}</title>
</head>
<body>
<include href="{{#content}}"/>
</body>
</html>
Your views/welcome.htm can contain anything.
<h1>Welcome</h1>
<p>You have arrived to the welcome page</p>
Please remember the folders views and layouts should be in the UI folder
$f3->set('UI','ui/');
On your comment:
You can access the params of a request from $f3->get('PARAMS') or you can do this.
$f3->route('GET /#page',
function($f3,$params) {
$page = $params['page'];
$f3->set('content','views/'. $page .'.htm');
$f3->set('page_head', 'Welcome');
echo Template::instance()->render('layouts/header+footer.htm');
}
);
This should work
If you wish to update only a portion of a web page, without doing a full refresh, then you need to use AJAX.
Start by reading this:
https://fatfreeframework.com/3.6/routing-engine#AJAXandSynchronousRequests
You'll need to use javascript on the front end to make this work. If you don't know how to code this in javascript, then you can start by reading this:
https://www.w3schools.com/js/js_ajax_intro.asp
You may find it easier to use Jquery, which is a very popular javascript library. You can read about it here:
https://www.w3schools.com/jquery/jquery_ref_ajax.asp
Maybe will be not bad to create your own instance of View and extend the main with functionality similar to PHPTal Filters. Then you can set Header in preFilter and Footer in postFilter and extend render() function. The extended render() will render everything together for you including header, footer and body content.
I don't have personally anything against FatFree View but will suggest PHPTal as a templating engine - here simply implementation https://github.com/creoLIFE/FatFree-PHPTAL including Header/Footer served from separate files.

Web Design; Same Object on Multiple Pages?

I apologize of this question has been asked before. I tried searching around, but was unable to find a relevant answer (probably due to my relatively small "web-design vocabulary").
I've noticed that the majority of websites have at least one--if not more--standard "objects" (or whatever the actually name is for them) on almost all of their pages. For instance, Stack Overflow has the same logo and tabs (Questions, Tags, Users...) on every page. I'm assuming that there's a less painstaking way to set this up other than simply copying and pasting the same code over and over, especially when ease of modification becomes a factor. As far as I know, CSS can't do accomplish this level of style generalization, so I'm assuming a server-sided language like PHP is part of the equation.
I'm not really looking for a very specific answer. What language--or type or language--as well as a brief synopsis of at least one way to achieve some sort of "object pasting" will be sufficient.
Like others said, this is a major reason why people go from HTML to something like PHP, at first just to split up parts of your page.
Yes, you can do exactly that. What I usually do (if I'm not using a framework) is create a folder in my directory like this:
inc/header.php
inc/footer.php
inc/menu.php
index.php
Then in index.php you'd need an include like:
<? include('inc/header.php'); ?>
<h2>Welcome to my site</h2>
<p>We're happy to have you</p>
<? include('inc/footer.php'); ?>
And in inc/header.php:
<!DOCTYPE html>
<html>
<head>
<title>My site</title>
</head>
<body>
And in inc/footer.php:
<div id="footer">
<h2>Thanks for visiting</h2>
</div>
</body>
</html>
And so on for inc/menu.php
Then for other pages on your site, do the same includes for header, footer, and menu, and just write your page-specific content between the includes
Just an alternative to PHP:
Use Javascript or jQuery.
$( "#footer" ).load( "includes/footer.html" );
Another alternative is to use SHTML, which is basically HTML with inserts.
An easy way to do this is to create separate files for different sections of your page then instead of pasting the same code on each page use
include ('yourfilename.php');
to add the code in yourfilename.php at that point in the php file. This also makes it easy to modify that section and have your changes be reflected on all the pages that use yourfilename.php
For example, you can make one file called page_top.php and another called page_bottom.php. Then on each of your various php pages you can include('page_top.php'); near the top and include('page_bottom.php'); near the bottom. The code within these files will then be executed on each of your content pages.
There are of course other methods but this is a super easy way and you should look into this first.
An example of include would be:
header.php
<!DOCTYPE html>
<html>
<head>
<stuff><stuff>
</head>
<body>
<div id="mybanner">Design and logo that is common on all pages</div>
content/contact.php
<div id="bulk_of_the_html">
The rest of your stuff goes here
</div>
foot.php
<div id="footer_common_to_all">This is your footer content that is common to all pages</div>
</body>
</html>
To use would be something like:
contact.php
// This is your common to all pages header
include("header.php");
// This can be changed up as content switches
include("content/contact.php");
// This is your common to all pages footer
include("foot.php");
HTML imports or Webcomponents is a new way to do this completely at client side using HTML, JS and CSS. Write a component and reuse it in every page. But it uses ShadowDom, means not search indexable yet.
<link rel="import" href="header-banner.html">
<!-- use this in body -->
<header-banner></header-banner>
You have two solutions
Use include('....php') or require('....php') or include_once('....php') or require_once('....php') php functions to add external sections/modules into your web page(php).
You can call this functions at the position where you want the extremal module/part to be appeared.
include("Header.php"); // call to external module
// your body goes here
<h1>.......</h1>
<p>........</p>
.....................
include("Footer.php"); // again to another module
Or its better if you can go for a MVC framework where you can combine multiple modules and views into one output page...(ex Codeignitor/Cakephp...)

Do search engines have trouble with urls like somesite.com/index.php?page=photos

I'm new to php coding, web development, and search optimization - so newbie overall. In the process of learning php and web development I've been trying out different website architectures and layouts. One I'm working on uses an approach like the following:
I have one index.php page which always loads a header.php, sidebar.php, and footer.php. The index.php also contains a switch so that depending on the page variable the index.php is passed it loads different core content. So for example a examplesite.com/index.php?page=photos and examplesite.com/index.php?page=stories would both have the same header, footer, and side bar but one would have photos and one would have stories as the main content.
<?php $page = $_GET['page'];?>
<?php include("header.php"); ?>
<?php include("nav.php"); ?>
<?php
switch ($page)
{
case 'play':
include("photos.php");
break;
case 'cards':
include("stories.php");
break;
default:
include("frontpage.php");
}
?>
<?php include("footer.php"); ?>
My navigation is made up of href="index.php?page=..." links so choosing a menu button on the index page essentially calls itself passing it the new core to load.
I have no idea if this is a totally unorthodox approach but it all started because I was initially going to create a wordpress theme but then halfway through decided not to do it in wordpress.
What i'm concerned about are what drawbacks could be associated with this approach when it comes to search engines, indexing, seo etc.
What are other drawbacks or issues I should be thinking about that maybe I'm not?
Thanks in Advance!
I have no idea if this is a totally unorthodox approach
There is nothing essentially "unorthodox" in using a query string to load various pages. Billions of sites using this approach. Search engines can index such pages all right.
Nevertheless,
I have one index.php page which always loads a header.php, sidebar.php, and footer.php.
This is wrong concept.
Having an index.php file only to load header and footer makes no sense and makes your site plainly unusable.
Here are main faults in your design:
You're assuming that header.php would be called with the every page call. That's wrong.
You're assuming that header.php will always be static. That's wrong.
You forgot to create a template for the page itself.
The main rule everyone have to learn by heart:
Not a single character has to be sent into browser, until all data gets ready.
Why?
it's 2012 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
there is a thing called HTTP header. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.
Separating display logic from the business logic will let you use the same php code on many sites. You will have to change only templates and don't touch engine files. That's really great benefit.
Imagine you're going to make a custom <title> tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates.
So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
And these templates have to be called only when all business logic is done - i.e. you have got all your data ready.
An example layout is going to be like this:
.1. page itself.
it outputs nothing but only gathers required data and then calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.tpl.php";
include "template.php";
?>
.2. template.php which is your main site template,
consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
.3. and finally links.tpl.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
this way you'd need no index with includes at all

Converting a site from a PHP site into a full LemonStand e-commerce site

Brand new to LemonStand, and it's my front-runner for developing a client's web site.
This first candidate for utilizing LemonStand is a newly built web site I built in PHP.
I've got all of the non-e-commerce pages (things like about and contact) pulling from the LemonStand CMS.
But now I am trying to convert what were simple PHP includes to partials:.
Example:
<? include 'standard_include.php'; ?>
<? include 'header.php'; ?>
to LemonStand's
<? $this->render_partial('standard_include') ?>
<? $this->render_partial('header') ?>
I get an unhandled exception related to undefined variables:
This is what the beginning my page/template looks like
<?php
require_once('lib/php/configuration.php');
$pagetype = 'home';
$subpagetype = 'index';
$titleValue = 'Client Name';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $titleValue ?></title>
<? $this->render_partial('standard_include') ?>
</head>
<body>
<? $this->render_partial('header') ?>
Do the partials not render till later in the page load process (as compared to PHP includes)?
Am I utilizing partials incorrectly? If so, what do I do to put the PHP includes into the LemonStand backdoor system?
I've never used Lemonstand, but if its MVC works like other MVC systems, then you need to inject the variables needed by standard_include into the view. Something like:
$this->renderPartial('standard_include', array('pagetype'=>$pagetype, ...)) ?>
Otherwise the partial won't have access to the variables you defined in your parent template because the partial is rendered in a different context.
PHP include works differently. It just literally inserts the included file in place at that point before the script is executed.
I'm thinking the render_partial is happening after the pages start rendering, while the include happens prior, so it has a value for pagetype.
I'm thinking the solution is going to be to keep them includes. I just won't be able to have every file in their admin/backend system.
For common elements, the included templates with LemonStand don't seem to do anything but have them inline, which obviously isn't optimal for making a change across a site for something like a header or footer.

Categories