Allow php in wordpress page - php

I want to use the following code in my wordpress. How can I do that?
<?php
$theme_name = get_template();
$directory = "wp-content/themes/" . $theme_name . "/pdf/ECE340/";
$pdfs= glob($directory . "*.pdf");
foreach($pdfs as $pdf)
{
$link= substr($pdf,48,90);
?>
<?php echo $link; ?>
<?php
echo "\n\n";
echo "<br />";
}
?>

Below is the answer (taken from here):
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
Copy the above function at the end of your functions.php file.
But the best soluttion will be to create a custom template instead of using PHP in pages/widgets/posts. You will find more about wordpress custom templates here.

Login to your admin panel. Click on Appearance -> Editor from left side menu. Then find the Template where you want to add the PHP code from right side menu. Then you can add your PHP code in the editor.
NOTE: Make sure do backup before you do any editing. I'm not responsible for your mistakes.

Related

Get a variable from another file in wordpress

I'm kind of new to wordpress coding and I've been trying to get a variable from another file.
I have this variable $final_cat_url in /custom/last-category.php that I want to reuse in customtemplate.php.
I've read lots of explanations and the codex, but it's still not working.
I've tried to use the following code in customtemplate.php
get_template_part( 'custom/last-category', null, array('my_final_cat_url'=> $final_cat_url));
echo $args['my_final_cat_url'];
Can you help me with that? Thanks a lot.
Add this function to your functions.php file:
function includeWithVariables($filePath, $variables = array(), $print = true){
$output = NULL;
if(file_exists($filePath)){
// Extract the variables to a local namespace
extract($variables);
// Start output buffering
ob_start();
// Include the template file
include $filePath;
// End buffering and return its contents
$output = ob_get_clean();
}
if ($print) {
print $output;
}
return $output;
}
Instead of using get_template_part(), use this:
<?php includeWithVariables('file_to_include.php', array('final_cat_url' => $final_cat_url)); ?>
In the file you included:
<?php echo $final_cat_url; ?>

Dynamic home link for OpenCart

For example in Wordpress you can call to home with:
<?php echo home_url(); ?>
But in OpenCart I can't find a similar function. In my header this works:
<?php echo $base; ?>
But not on other templates in my theme. Someone who got a global function for this in OpenCart? Or a library to share, would be perfect!
I work with OpenCart 2.0
Try this,
<?php echo HTTP_SERVER; ?>
In the controller of the page, in my case footer.php I pasted this:
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
$data['base'] = $server;
And then in my template, footer.tpl I could use:
<?php echo $base; ?>
The correct way to do this in OpenCart is to use
<?php echo $this->url->link('common/home'); ?>
Note that this adds the full URL and route, not just / which is not possible using the SEO URL class without modification
<?php echo $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'); ?>
Copy from controller/common/home/header.php
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
and
$data['home'] = $this->url->link('common/home');
Add these to whichever controller needs them
Copy from header.tpl
href="<?php echo $home; ?>"
add to whichever template needs it.
Would that not cover SEO and link needs?

PHP script tags

Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.

PHP include html by option

I need to include html, not from file, just simple code.
I have main content included by:
<?php
$content = array(
'001'=>'content/001_001.php',
'002'=>'content/001_002.php',
'003'=>'content/001_003.php'
);
if(in_array($_GET['show'], array_keys($content))) {
include($content[$_GET['show']]);
} else {
include('content/001_001.php');
}
?>
And on a side i'd like to include simple html, but that's more like 3 buttons in each category, so cloning lots of *.html & *.php files won't be right and clean work.
In case of opened page: ?show=001, on a side would be added <div>001</div>;
?show=002, on a side would be added <div>002</div> and etc.
If I understand your question correctly, you want to load HTML code from an array rather than from a file. You can do this by changing your include to echo.
<?php
$content = array(
'001'=>'<div>001</div>',
'002'=>'<div>002</div>',
'003'=>'<div>003</div>'
);
if(!empty($_GET['show']) && isset($content[$_GET['show']])) {
echo $content[$_GET['show']];
} else {
echo $content['001'];
}
?>

How do I put a notice reminder when somebody remove the powered on my theme footer

Example link on my footer
$powered = 'Powered by Stackoverflow';
theme file
<?php echo $powered; ?>
How to make if $powered removed from my footer and the error/reminder notice it.
Example die('Do not remove the powered link')
Short answer: not possible.
Long answer:
<?php
$powered = 'Powered by Stackoverflow';
ob_start();
echo $powered;
$html = ob_get_clean();
/*
* If you remove the "echo $powered;" part, please don't remove the following lines
* so I can detect the license violation. Thank you for your cooperation!
*/
if( strpos($html, $powered)===FALSE ){
echo die('Do not remove the powered link');
}else{
echo $html;
}
?>
It's possible... you can use Zend Guard to protect your code from being altered.

Categories