How to remove a class from a link... on only one page? - php

I'm trying to create a 404.php page and would like to remove the "current state" class from a link, only on that page.
As of right now this is what I've got...
jQuery( function() {
if ( TEMPLATE_URI + '/404.php'.hasClass( '.current_page_parent' ) ) {
jQuery( '.menu' ).removeClass( '.current_page_parent' );
}
});
which does not seem to be working.
I am a beginner in the subject of jQuery so any help would be appreciated!

I think this should work :
if(window.location.href.indexOf('404.php') != -1){
jQuery('.current_page_parent').removeClass('current_page_parent')
}
Also, when using removeClass, you don't need to put the dot.

WordPress automatically uses the file 404.php to show 404 error pages. So you don't have to check for 404.php in your code.
Just put this Javascript code in the file 404.php, preferrably at the bottom:
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery('.menu').removeClass('current_page_parent');
});
</script>
Otherwise, you can use the function is_404() in any template (PHP) file to see if the current page is a 404 error page. Something like this:
<?php if (is_404()): ?>
jQuery('.menu').removeClass('current_page_parent');
<?php endif; ?>

Thank you for the help #Christian Daven and #Karl-Andre Gagnon! Basically what I did was combine bits of both answers and got this (which I have placed in my 404 Template page)...
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery( '.current_page_parent' ).removeClass( 'current_page_parent' );
});
</script>

Related

wordpress contact form7, one form redirect to multiple thank you page based on is_page

I am looking for a solution on wordpress contact form7.
either a plugin or in PHP code.
I am not looking for JavaScript solution. as I have already found JavaScript solution. Again: I am looking for either plugin or PHP code.
I want to use one form (CF7) and it will redirect to multiple thank you pages based on from which page the form submitted.
Below example code.
if is_page(1){
contact-form will go to thank-you-page-1
} else if is_page(2) {
contact-form will go to thank-you-page-2
}
You can do by this hook
add_action('wpcf7_mail_sent', function ($cf7) {
// Run code after the email has been sent
$wpcf = WPCF7_ContactForm::get_current();
$wpccfid=$wpcf->id;
// if you wanna check the ID of the Form $wpcf->id
if ( '34' == $wpccfid ) { // Change 123 to the ID of the form
//you can use also if(is_page()){} condition
//redirect to url
wp_redirect('url of thank you page');
exit();
}
}
<?php if (is_page(array(1))) { ?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'https://page-link-1.com/';
}, false );
</script>
<?php } else if ( is_page(array(2))) { ?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'https://page-link-2.com/';
}, false );
</script>
<?php } ?>

How to auto-scroll down when my page loads In Wordpress?

I write the below code to a footer.php file. This works but is applied for all the pages. I want to apply page scroll in one page only.
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('body').animate({scrollTop: +400}, 1000);
});
</script>
Can any one help me on how to do it.
Thanks
Add a contidional statemnent on the footer.php file to only execute the jquery code if it is in only in the current page you wish for.
something like this example below
<?php
global $post;
$target_pageid = 7654;
$current_pageid = $post->ID;
if (current_pageid == $target_pageid){
?>
jQuery(document).ready(function() {
jQuery('body').animate({scrollTop: +400}, 1000);
});
<?php
}
?>

Use jQuery in external php file

I have a php file that I load into another php file with jQuery. This works, but the moment I start using jQuery in the 'external file', I get ERROR 500.
The reason I used this approach is because this is handy to refresh the data after an AJAX function.
This I have:
test.php:
<script type="text/javascript" src="js/modernizr.custom.29473.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function () {
$(document).tooltip({
items: ".plupic , .ingr",
content: function() {
var element = $( this );
if ( element.is( ".plupic " ) ) {
var src = element.attr('src');
return "<img src='" + src + "' style='max-height: 300px; max-width: 300px;'>";
}
if ( element.is( ".ingr" ) ) {
var txt = element.text();
return txt;
}
}
});
$('#kasticket').load('cart.php');
});
</script>
</head>
<body>
<div class="container">
<div id="kasticket"></div><!-- Load data with jQuery-->
cart.php:
I just do a select from the database and write some data to a table with echo();
This works perfectly, but the moment I want to use jQuery, I goes all wrong...(I know this for sure because the jQUery works in a local html file and putting this line in comment makes my php working again)
echo("
<script>
jQuery(document).ready(function() {
if($('#L$MyAant').width() < 70) {
$('.TR1$MyAant').show();
$('.TR2$MyAant').hide();
}else{
$('.TR2$MyAant').show();
$('.TR1$MyAant').hide();
}
});
</script>
");
I have no idea what I'm doing wrong.
If its any help: http://www.itreflex.be/TestAcc/test.php (with currently the jQuery line in comment).
And this is cart.php, exported to txt, it was to long to paste here.
hard to tell without the full source code but I have got a couple of ideas:
First Error 500 should be the HTTP code for internal server error, which basically means that the error lies on the server, then on the PHP side.
Could it be possible that you are mixing up PHP and jQuery on some of your other statements not posted here?
Second, you missed a single quote on your line
$('#kasticket').load(cart.php');
In your cart.php remove the brackets after echo ... For example
echo "<script>
jQuery(document).ready(function() {
if($('#L$MyAant').width() < 70) {
$('.TR1$MyAant').show();
$('.TR2$MyAant').hide();
}else{
$('.TR2$MyAant').show();
$('.TR1$MyAant').hide();
}
});
</script>";
Try this above line in your cart.php and see if that works.

New to jQuery - why isn't this simple function working?

I'm building a web application inside CodeIgniter and I've decided to have a fiddle with jQuery and Javascript - something I've never really used before.
I have included jQuery in my header using Google Libraries:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
And now I'm fiddling with the SIMPLEST of jQuery but it's not working.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
<script type="text/javascript">
$("#lovecounter").click(function() {
alert("Click");
});
</script>
I am clicking on the lovecounter link but there is no alert. If I add simple javascript to the actual anchor tag it works though.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
Any ideas? What am I doing wrong?
Wrap your code in ready handler:
$(document).ready(function(){
$("#lovecounter").click(function() {
alert("Click");
});
});
Working Example
This works perfectly fine for me:
<p>asdf</p>
<script type="text/javascript">
$(document).ready(function() {
$("#lovecounter").click(function() {
alert("Click");
});
});
</script>
You should include a $(document).ready(function() {}); just like Sarfraz said. Otherwise, it would not find anything with an id of lovecounter because nothing on the page has loaded yet. If you wait for it to load, then it will find the element. Here is some documentation: http://www.w3schools.com/jquery/event_ready.asp. Here is a full jQuery lesson: http://www.codecademy.com/tracks/jquery.

Why is my PHP code not working?

I have made a code --
<?php
header("Content-type: text/javascript");
require_once('../php/config.php');
$domail_theme = $_GET['theme'];
?>
<?php
if($domail_theme=='default')
{
?>
$('div.theme_content').mouseover(function () {
$(this).stop().animate({
backgroundColor : '#234'
}, 250, 'linear', function() { });
});
$('div.').mouseout(function () {
$(this).animate({
backgroundColor : '#ddd'
}, 400, 'linear', function() { });
});
<?php
}
?>
And here is the script including tag --
<script src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>
The thing I want is that when I mouse over on the div element with class theme_content it's background changes according to the given one in the animate function. It is working for the input element but that script is original javascript and in this I have included php that's why I'm thinking whether the php code I used is wrong or my javascript. Also when I include this code in javascript file it works correctly. And by the I'm including the script tag in between the body tag, is it wrong? Please help me out with this one.
Thanks in advance!
Try to put it inside script. <script></script>
And also it's possible that DOM might not have been loaded. so try putting wrappint in $(document).ready(function() { /*Your code here*/})
And also put alert('hello'); to check if that code is being executed or not. It might be useful in debugging.
The most likly explanation is that 'theme" is not set to 'default' in your URL request.
This should be easy to check. Also you can "View" -> "Page Source" in your browser and look at the html your php program is actually generating. If my guess is right you won't see your mouseover function in the page.
Hi you should write like this:-
<?php
if($domail_theme=='default')
{
?>
<script>
$('div.theme_content').mouseover(function () {
$(this).stop().animate({
backgroundColor : '#234'
}, 250, 'linear', function() { });
});
$('div.').mouseout(function () {
$(this).animate({
backgroundColor : '#ddd'
}, 400, 'linear', function() { });
});
</script>
<?php
}
?>
Just need add tag between on your php if statement.
Hope can help you.
Does it work better if you add type='text/javascript' to your script tag?
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>
Why not just put the php logic in your main file and include the javascript as plain javascript?
<?php
if($domail_theme=='default')
{
?>
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.js"></script>
<?php
}
?>
Seems like overcomplicating things

Categories