I would like to show different header images on a wordpress site using php. Although I found numerous subjects with the title of my question, none of them clearly explain how this is done.
The first one I tried just failed to work. Here is the code I added to functions.php. I know the file is executed since the text I added is displayed.
//12/28/2020 - START
define("HOME", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/nyc-e1608682430748.png");
define("AWARD", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/trophy-e1608681301876.jpg");
define("ERP", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/IntuitiveERP-e1608681626647.png");
define("EVENT", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/Calendar-scaled-e1608681638194.jpg");
function change_the_header($url_for_image) {
if (is_home())
$url_for_image = HOME;
else if (is_single())
$url_for_image = ERP;
else
$url_for_image = AWARD;
return $url_for_image;
}
add_filter('theme_mod_header_image', 'change_the_header');
//12/28/2020 - END
This one doesn't explain where the code goes nor how the function is_page works. I tried to fill in the blanks, but it failed in the end.
//12/28/2020 - START
if( is_page('About') ) echo HOME;
elseif( is_page('Events') ) echo EVENT;
elseif( is_page('Awards') ) echo AWARD;
else echo '-NONE-';
//12/28/2020 - END
With one answer by someone who knows nothing of Wordpress and no mention of which file is being edited, I didn't even attempt this.
With code that actually ran, I had the most hope here. However, there is no explanation given on how to determine what page you are on. There is no is_about function nor is there any information on what is required of one so I can create it. And once I have it, there is nothing to indicate where it goes. Also passing a parameter to get_header does nothing.
//12/28/2020 - START
//12/28/2020 - OLD
//get_header();
//12/28/2020 - NEW
if (is_home()) {
echo '* HOME *';
get_header();
//} else if (is_about()) {
// echo '# away #';
// get_header('About');
} else {
echo '# away #';
get_header('About');
}
wp_head();
//12/28/2020 - END
I figured it out: The id can be found in the body tag or in your browser when using wordpress to edit pages, it will be the post id.
in the following link the page id is 62:
johnm5.sgedu.site/wp-admin/post.php?post=62&action=edit
Here's my code:
//JPM - 12/28/2020 - START
define("ABOUT_ID", 6);
define("AWARD_ID", 64);
define("ERP_ID", 58);
define("EVENT_ID", 62);
define("ABOUT", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/nyc-e1608682430748.png");//68
define("AWARD", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/trophy-e1608681301876.jpg");//64
define("ERP", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/IntuitiveERP-e1608681626647.png");//58
define("EVENT", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/Calendar-scaled-e1608681638194.jpg");//62
function change_the_header($url_for_image) {
if (is_page(ABOUT_ID)) {
$url_for_image = ABOUT;
} else if (is_page(AWARD_ID)) {
$url_for_image = AWARD;
} else if (is_page(ERP_ID)) {
$url_for_image = ERP;
} else if (is_page(EVENT_ID)) {
$url_for_image = EVENT;
}
else {
echo "<script>alert('- NONE -');</script>";
}
return $url_for_image;
}
add_filter('theme_mod_header_image', 'change_the_header');
Related
I want to make a call of this php logic inside a html div but when passing it as a function the logic breaks since it does not send an error message in case of entering the pass wrong and its confirmation at the time of performing the password change.
<?php
require 'funcs/conexion.php';
require 'funcs/funcs.php';
$user_id = $mysqli->real_escape_string($_POST['user_id']);
$token = $mysqli->real_escape_string($_POST['token']);
$password = $mysqli->real_escape_string($_POST['password']);
$con_password = $mysqli->real_escape_string($_POST['con_password']);
if(validaPassword($password, $con_password))
{
$pass_hash = hashPassword($password);
if(cambiaPassword($pass_hash, $user_id, $token))
{
echo "Contraseña Modificada <br> <a href='index_alumnos.php' >Iniciar Sesion</a>";
} else {
echo "Error al modificar contraseña";
}
} else {
echo "Las contraseñas no coinciden <br> <a href='index_alumnos.php' >contacta a Academia</a>";
}
?>
If the echo happens before your actual div is drawn, the echo goes... right where it happens. Which isn't within your div.
One way of getting around this would be to put your error message into a variable and then deliver this variable into your div (whether it be through a return value, if it's a call, or some other means.)
Here's a simple example to illustrate this:
<?php
if(1 === 2) {
//great, 1 is 2
} else {
//oh no, an error
$someErrorLine = '1 is not 2';
} ?>
<h1>Hi</h1>
<div><?= $someErrorLine ?></div>
You could also check if the variable exists, something like if(isset($someErrorLine)) {} and echo the div with it, or put the div within your variable.
I'm working on a Captcha class and i'm almost done, there is one thing that doesn't work
In the file where I put the form, I start with this line:
include 'captcha.php';
$captcha = Captcha::tryCaptcha(2,4,'#000', '#ffffff');
and this is the captch construct:
static $do_generate = TRUE;
function __construct($aantal_letters = 2, $aantal_cijfers = 4, $voorgrond = '#000000', $achtergond = '#ffffff') {
session_start();
if (self::$do_generate == TRUE) {
$letters = substr(str_shuffle('ABCDEGHJKLMNPQRSTUVWXYZ'),0 ,$aantal_letters);
$cijfers = substr(str_shuffle('23456789'),0 ,$aantal_cijfers);
$imgbreed = 18 * ($aantal_letters + $aantal_cijfers);
$_SESSION['imgbreed'] = $imgbreed;
$_SESSION['captcha'] = $letters . $cijfers;
$_SESSION['voorgrond'] = $this->hex2rgb($voorgrond);
$_SESSION['achtergond'] = $this->hex2rgb($achtergond);
}
}
so in other words I put my stuff in a session if the static var $do_generate == TRUE
So when I post the form, the captcha is getting checked by a procesor.php
like this:
if (Captcha::captcha_uitkomst() == TRUE) {
echo "Great";
} else {
echo "Wrong";
}
And this is the captcha function that checks the etered captcha code:
static function captcha_uitkomst() {
if (strcmp($_SESSION['captcha'], strtoupper(str_replace(' ', '', $_POST['captcha-invoer']))) == 0) {
return TRUE;
} else {
echo "test";
self::$do_generate = FALSE;
return FALSE;
}
}
If I enter a correct captcha code, it's all good, that works I get the echo great.
If wrong I get the echo Wrong,
Perfect, but.... when I go back to form (hit backspace one history back) to enter a correct captcha, it regenerates a new captcha.
In the class: captcha_uitkomst you see that I made the self::do_generate FALSE
And the echo 'TEST' works when it's false, (just for checking)
What am I doing wrong
When you hit "back", the page is reloaded. You get a new CAPTCHA.
The premise of your question is fundamentally flawed, as you have just randomly assumed that this shouldn't happen, whereas in reality this is entirely by design.
It wouldn't be a very effective CAPTCHA if you could repeatedly get it wrong then go back and try again; any bot could just start brute forcing it and learning from the experience.
i have a php code and when i add some gets variables i get error, 500, i tested it with ajax and without ajax(directly writing the url on the adress bar)
When i go with localhost/ajax/test.php?did=1 everything is fine but when i go with localhost/ajax/test.php?did=1&task=1 the problem happens
all the functions are created before on the ../php/core.php
Here is the code
<?php
require '../php/core.php';
$did = floor($_GET['did']);
if (device_exist($did) && amlogedin() && intouch_get($uid, $did) == 2) {
$task = floor($_GET['task']);
$id = safestring($_GET['id']);
switch ($task) {
case 1:
if (feature_removefeature($did, $fid)) {
echo donemsg("Feature Removed");
}
break;
}
design_makefeturelist($did);
}
else {
echo 'Sorry, some thing is wrong';
}
Almost sure that you've got an error in the feature_removefeature or donemsg function. Because after you set task=1 it will enter the case 1: statement. You could replace the if with a simple echo "lala"; to check it out.
ps. Is $fid already set?
I am working on a webshop for school.I'm trying to make an jquery/ajax cart system.
Now this is my jquery.load(); code
//LOAD PHP FUNCTION TO ADD TO CART
$("#cart_page").load("cart.php?command=addToCart&productID="+productID+"&size="+size+"&amount="+amount+"&product_name="+product_name+"");
This is the cart.php file:
<?php
//ADD PRODUCTS TO CART
if($_GET['command'] == 'addToCart'){
/********* START SESSION *********/
session_start();
if($_GET['productID'] == "" || $_GET['amount'] < 1 || $_GET['size'] == "" || $_GET['product_name'] == "") return;
$max=count($_SESSION['cart']);
if(is_array($_SESSION['cart'])){
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
if($_GET['productID'] == $_SESSION['cart'][$i]['productid']){
if($_GET['size'] == $_SESSION['cart'][$i]['size'] ){
$product_exists = 1;
}
}
}
if($product_exists != 1){
$_SESSION['cart'][$max]['productid'] = $_GET['productID'];
$_SESSION['cart'][$max]['product_name'] = $_GET['product_name'];
$_SESSION['cart'][$max]['amount'] = $_GET['amount'];
$_SESSION['cart'][$max]['size'] = $_GET['size'];
}
}else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid'] = $_GET['productID'];
$_SESSION['cart'][0]['product_name'] = $_GET['product_name'];
$_SESSION['cart'][0]['amount'] = $_GET['amount'];
$_SESSION['cart'][0]['size'] = $_GET['size'];
}
if(is_array($_SESSION['cart'])){
$max=count($_SESSION['cart']);
$cart = <<<EOD
<div class="cart_page">
<div class="cart_product_line">
$max
<ul>
EOD;
for($i=0;$i<$max;$i++){
$arrayID = $i;
$cart_productID = $_SESSION['cart'][$i]['productid'];
$cart_productName = $_SESSION['cart'][$i]['product_name'];
$cart_amount = $_SESSION['cart'][$i]['amount'];
$cart_size = $_SESSION['cart'][$i]['size'];
$cart.= <<<EOD
<li>$arrayID</li>
<li>$cart_productID</li>
<li id="$cart_productID">$cart_productName</li>
<li class="amount">$cart_amount</li>
<li class="size">$cart_size</li>
EOD;
}
$cart.= <<<EOD
</ul>
</div>
</div>
EOD;
}
}
echo $cart;
?>
Now it does everything i want it to. But the result it gives in 'echo $cart.php' doesn
't get displayed in my index.php
can you guys help me out please?
PS:
if i go to:
cart.php?command=addToCart&productID=28&amount=10&size=12&product_name=Test
It gives this back
1
0
28
Test
10
12
in HTML so the script works and gives html back!
I'm doing the same with loading products and there it works like a charm.
Assuming that everything you have said is true then there are several things that might go wrong here (in order of likelyhood):
Is the AJAX call being made to another domain? If so this won't work unless you output some special headers.
jQuery.load will fail if the element you are targetting does not exist. So double check that you have the #cart_page element on your page.
The AJAX page is not returning the correct HTTP response code. This will be visible in your console. But you can get more information by expanding your code to:
$("#cart_page").load("cart.php?command=addToCart&productID="+productID+"&size="+size+"&amount="+amount+"&product_name="+product_name+"", function(response, status, xhr) {
if (status == "error") {
console.log("Error code :" + xhr.status); // error code
console.log ("Error text :" + xhr.statusText); // error text
}
});
Some other JS errors. As we cannot see all of your code this is hard to see. Check the console for any errors and add them to your first post.
(Sorry for the formatting of the code in point 3. I've no idea how to embed code in a list using this syntax. An indent doesn't work)
So So Stupid..
The $product_name that I generated was from a database.
This name had a " " space in it.
What this did was it gave the success of loading the script but because it had a " " space in the URL jQuery sees it as a incorrect URL and doesn't embed the script into the page.
What I find strange is that it didn't give me any error back!?
Thank you guys so much for helping me out!
This issue came up to me in vBulletin system. i want to build a block in sidebar area to help me autmate some daily works.. i wrote php code like this :
<?php
$myDay = date('D');
$myHour = date('G');
// Saturday
if ($myDay == "Sat") {
if ($myHour >= 7) {
$output = include('textFilePath/saturday.txt');
} else {
$output = include('textFilePath/friday.txt');
}
}
// Sunday
if ($myDay == "Sun") {
if ($myHour >= 7) {
$output = include('textFilePath/sunday.txt');
} else {
$output = include('textFilePath/saturday.txt');
}
}
// and it goes on to friday...
// and in the end :
return $output;
?>
my problem is with include() function . when i return the $output value it returns a boolean(0 or 1) and include function writes out the txt files content in the beginning of document instead of "sidebar block"
i know i can echo the actual content instead of using include and txt files. but as i said i want to automate the daily tasks and give the users access to edit the txt files.
is there any other technique or function to assign the content of a local file to a variable ?
you may want to check
$output = file_get_contents('textFilePath/saturday.txt');
more information here : http://in3.php.net/file_get_contents