Using Page $data variables in template - php

Is there a way to use page $data variables in the template it's loaded in?
For instance - if I have a template that has a head like this:
<!DOCTYPE html>
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<title><?php echo $data['title']; ?> | ACME, Inc.</title>
</head>
My view is pages/home and the controller looks something like this:
public function index($data) {
$data = array();
$data['title'] = 'Home';
$this->load->view('pages/home', $data);
}
How do I get a parent template or sibling view to take on the page's variables?

I usually use the code below for meta data in page
In controller:
$page = new stdClass();
$page->title = "Your Page Title";
$page->desc = "Your page description";
$page->key = "keword1,keyword2,keyword3";
$page ->author = "Alex";
$data['page'] = $page;
$this->load->view('pages/home', $data);
In View:
<title><?php
if(isset($page->title)){
echo $page->title;
}
?></title>
<meta name="description" content="<?php
if(isset($page->desc)){
echo $page->desc;
}
?>"/>
<meta name="keywords" content="<?php
if(isset($page->key)){
echo $page->key;
}
?>"/>
<meta name="author" content="<?php
if(isset($page->author)){
echo $page->author;
}
?>"/>
content="<?php
if(isset($page->desc)){
echo $page->desc;
}
?>"/>
This is safe for not conflicting variable and easy to use.

Related

Use BLOB image from MySQL db for meta og:image to show preview image to facebook

I can get a BLOB image from my MySQL database but I want to show it as a preview image when I post to facebook. I don't want to save the image to my computer because I will end up with too many images. The closest I can get is showing a load of ascii code instead of the actual image. Is it even possible to achieve this? This is the code I have at the moment.
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/includes/config.php");
$keys = htmlspecialchars($_GET['id']);
$query = "SELECT * FROM birdtabletop WHERE id='$keys' AND approvalMsg = 'Approved' LIMIT 1";
if(!isset($error)){
try {
foreach ($db->query($query) as $row) {
//set vars here
$tTitle = $row['tabletop_name'];
$tPicture = $row['tabletop_photo'];
}
} catch(PDOException $e) {
header('Location: login.php');
}
}
if(!isset($row)){
header('Location: login.php');
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<?php include_once("includes/analyticstracking.php") ?>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php echo $tTitle; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="">
<meta property="og:title" content="<?php echo $tTitle; ?>">
<meta property="og:type" content="website">
<meta property="og:url" content="https://www.finchkeeper.com/viewtabletop.php<?php echo "
?id=".htmlspecialchars(filter_input(INPUT_GET, 'id')); ?>">
<?php
if($tPicture != NULL){
echo '<meta property="og:image" content="data:image/jpeg;base64,'.base64_encode( $tPicture ).'"/>';
}else{
echo '<meta property="og:image" content="https://www.finchkeeper.com/images/tabletop.jpg">';
}
?>
<meta property="og:description" content="" />

How do I get the template file for a single.php pods page?

I am currently using a template file called single-product.php and want to check for page template in header.php. I've tried using get_page_template_slug($post->ID) but it returns empty. I've also tried is_page_template('single-product.php'); but that returns nothing, as well.
Here's my header. You'll notice that I'm trying to get the template file through s_page_template('single-product.php') ny setting it as the value of $isProductPodTemplate at the top and use it in the if statement at the bottom:
<?php
$page = get_the_title($post->ID);
$frontpageId = get_option('page_on_front');
$isStandardTemplate = is_page_template('standard.php');
$isContactTemplate = is_page_template('contact.php');
$isProductPodTemplate = is_page_template('single-product.php');
?>
<!DOCTYPE html>
<html id="mainHTML" lang="en">
<head>
<title>Architectural Iron Works</title>
<meta name="description" content="Architectural Iron Works">
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel=icon type="img/ico" href=/favicon.ico> -->
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/main.bundle.css" />
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<?php wp_head(); ?>
<script type="text/javascript">
document.getElementById("mainHTML").style.display = "none";
</script>
</head>
<body>
<!-- Header -->
<?php
if ($post->ID == $frontpageId)
{
get_template_part('/includes/_home-header');
echo "<div class='main-content'>";
}
else if (is_404() ||
$isContactTemplate ||
$isStandardTemplate)
{
get_template_part('/includes/_no-banner-header');
echo "<div class='main-content no-banner'>";
}
else if ($isProductPodTemplate)
{
// do something
}
else
{
get_template_part('/includes/_default-header');
echo "<div class='main-content'>";
}
?>

How to write variables in a php file with html in it?

Hello everyone.
I've got a small problem. Instead of making a $content variable and putting in the whole content there, I would like to set the page up like this (below), though the problem is that the php variables that is placed at the end (title, keywords, description, page), aren't working even though its in the index.php.
Question: How can I make the variables work without making a few head_template.php with the variables filled out already?
index.php:
<!DOCTYPE html>
<html class="html" lang="en">
<?php require_once "head_template.php"; ?>
<body class="<?php echo $page; ?>">
<?php require_once "navigation_menu_template.php"; ?>
<?php require_once "load_template.php"; ?>
<h1>Content</h1>
<?php require_once "cookies_script_template.php"; ?>
<?php require_once "footer_template.php"; ?>
</body>
</html>
<?php
$title = "QCG | Homepage";
$keywords = "QCG, Homepage";
$description = "QCG | Homepage - Description";
$page = "homepage";
?>
head_template.php:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
You are setting them after they are used. If you put that block at the top of your page it will work.
<!DOCTYPE html>
<html class="html" lang="en">
<?php
$title = "QCG | Homepage";
$keywords = "QCG, Homepage";
$description = "QCG | Homepage - Description";
$page = "homepage";
?>
<?php require_once "head_template.php"; ?>
<body class="<?php echo $page; ?>">
<?php require_once "navigation_menu_template.php"; ?>
<?php require_once "load_template.php"; ?>
<h1>Content</h1>
<?php require_once "cookies_script_template.php"; ?>
<?php require_once "footer_template.php"; ?>
</body>
</html>

different title, keyword and description in every page's

How can I add a different title, keyword and description in every page's <head> of my simple php website dynamically?
I have included file header.php in all of my pages, how can i know in what page the user in?
For example, I have php files register.php, and login.php, and I need different titles, keywords and descriptions.
I do not want use the $_GET method.
Thanks!
Set variables at the top of each page that will be read by header.php. Then insert the values of the variables in the right places in header.php. Here is an example:
register.php:
<?php
$title = "Registration";
$keywords = "Register, login";
$description = "Page for user registration";
include('header.php');
?>
header.php
<html>
<head>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />
<title><?php echo $title; ?></title>
</head>
<body>
Put the output inside a function (within your header.php) and insert its parameters at appropriate places into the markup.
function html_header($title = "Default") {
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
</head>
…
<?php
}
you can try this:
for example the $page variable is your page name:
<?php
switch($page)
{
case 'home':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
case 'download':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
case 'contact':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
}
if(isset($title))
{
?>
<title><?php echo $title; ?></title>
<meta name="keywords" content="<?php echo $keyword; ?>" />
<meta name="description" content="<?php echo $desc; ?>" />
<?php
}
else
{
?>
<title>default</title>
<meta name="keywords" content="default" />
<meta name="description" content="default" />
<?php
}
?>

add opengraph tag in article page

how to add differents meta (opengrapg) tag to every dynamic created page (www.page.com/index.php?page=article&id=1), i tried to put this in article page:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta property="og:title" content="<?php echo $title?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://www.page.com/index.php?page=article&id=<?php echo $id ?>" />
<meta property="og:image" content="http://www.page.com/image.png" />
<meta property="og:site_name" content="eeeeee" />
<meta property="og:description" content="<?php echo $desc; ?>"/>
<meta property="fb:admins" content="blabla" />
</head>
but FB Linter cant see them - "Required Property Missing"
I simple include with switch function article.php page
$conlibrary="play/pages/" ;
IF(!isset($_GET['page'])){
$page = 'deafault';
} ELSE {
$page = $_GET['page'];
$findme = '&';
$pos = strpos($page, $findme);
IF ($pos ===true) {
$data = explode("&", $data);
$dest =$conlibrary."/".$data[0].".php";
IF (file_exists($dest)) {
$page = $_GET['page'];
} ELSE {
$page = '404';
}
} ELSE {
$dest =$conlibrary."/".$page.".php";
IF (file_exists($dest)) {
$page = $_GET['page'];
} ELSE {
$page = '404';
}
}
}
include($conlibrary . $page .".php");
ty
Your missing the ? in your URL...
www.page.com/?page=article&id=1
not
www.page.com/page=article&id=1

Categories