It is possible to add php to this script in Wordpress? - wordpress

I use this script to filter posts from single category. I based on tags which generate also a class. It is possible to generate this script by WordPress with all tags from my site?
For example below 2 scripts (now I have 12 scripts of this type):
$(document).ready(function(){
$('tr').show();
$("#aipa").click(function(){
$("tr").show();
$('tr:not(:first)').not(".aipa").slideToggle('fast');
});
});
$(document).ready(function(){
$('tr').show();
$("#ris").click(function(){
$("tr").show();
$('tr:not(:first)').not(".ris").slideToggle('fast');
});
});
Now when I add new tags I must manually add another script:
$(document).ready(function(){
$('tr').show();
$("#next-tag").click(function(){
$("tr").show();
$('tr:not(:first)').not("#next-tag").slideToggle('fast');
});
});

Here's a way to do this. This assumes that you're using the standard blog tag system, and that your variable you're using as your ID and class is the tag's slug. Alter it as needed for your needs.
<?php $tags = get_tags(); ?>
<script>
$(document).ready(function(){
<?php foreach ( $tags as $tag ) { ?>
$('tr').show();
$("#<?php echo $tag->slug; ?>").click(function(){
$("tr").show();
$('tr:not(:first)').not(".<?php echo $tag->slug; ?>").slideToggle('fast');
});
<?php } ?>
});
</script>
This will create a single document ready function, and then generate multiple click functions.

Related

Is there a way to use wp_enqueue_script for adding JavaScript directly?

Is there a way of adding Javascript to a page directly but having Wordpress to wait for jQuery to load? Just as with using wp_enqueue_script?
In the custom page template I have the following code:
<?php if (isset($_GET['status']) && $_GET['status'] == 'newly_created') : ?>
<script>
$(function() {
App.dialogs.afterPetitionCreate();
});
</script>
<?php endif; ?>
Wordpress barks that $ is undefined. Now I do not want to load a entire .js file just to call that function. So in that sense wp_enqueue_script isn't an option. I only want to fire some JavaScript directly on the page but I do want to wait for jQuery to load.
This is what I also tried, adding the JavaScript to the footer with Wordpress:
<?php if (isset($_GET['status']) && $_GET['status'] == 'newly_created') :
add_action('wp_footer', 'my_footer_scripts');
function my_footer_scripts() { ?>
<script>
$(function () {
App.dialogs.afterPetitionCreate();
});
</script>
<?php } ?>
<?php endif; ?>
But unfortunately the same error appears $ is undefined.
So what would be the correct way to quickly load some Javascript on a custom Wordpress page template AND wait for jQuery to load?
I do not think this is a good practice.
It is better to change logic to something like this:
<?php if (isset($_GET['status']) && $_GET['status'] == 'newly_created') : ?>
<input type='hidden' id='status' name='status' value='newly_created'>
<?php endif; ?>
script.js:
$(function() {
if ( $('#status').val() === 'newly_created' ) {
App.dialogs.afterPetitionCreate()
}
});
and
wp_enqueue_script( 'afterPetitionCreate', '/path/to/script.js', array( 'jquery' ), '', true );
p.s. you can wrap wp_enqueue_script() into is_page_template( 'my-cool-page-teplate.php' ).
Have you tried running the script within the document ready event like the following code below?
<script>
jQuery(document).ready(function($){
$(function () {
App.dialogs.afterPetitionCreate();
});
});
</script>
The .ready event occurs when the object of the document or the DOM is loaded which makes it perfect to place all of your jquery events and other functions.
This is the code I ended up writing, and does exactly what I want. Like Submit Parkash mentioned, jQuery needs to be used instead of $.
/*
* Adds a single bit of Javascript to the page.
*/
function theme_embed_script($script) {
add_action('wp_footer', function ($script) {
echo '<script type="text/javascript">' . $script . '</script>';
}, 10);
do_action('wp_footer', $script);
}
Usage:
<?php theme_embed_script('jQuery(function () { alert("Hello"); });'); ?>
OR use Wordpress' wp_add_inline_script() script like David mentioned.

How to add a nonce to an inline script in WordPress

I'm trying to add a nonce to the inline scripts inserted in WordPress by wp_add_inline_script and wp_localize_script, but I can't figure out how to do it. It looks there are no WordPress filters for that. My goal is to have a nonce for inline scripts, so I can define a Content Security Policy that would not break common plugins that insert inline scripts. At the end the result should be something that looks like:
<script type="text/javascript" nonce="xxxxxxxxxx">....</script>
where xxxxxxxx is the nonce.
Do you have any ideas?
As the HTML for inline scripts are generated by the WordPress code
sprintf( "<script type='text/javascript'>\n%s\n</script>\n", ... )
you cannot add an attribute to the HTML script element using wp_add_inline_script() as <script type='text/javascript'> is hard coded.
However, the filter 'script_loader_tag' will allow you to change the HTML for script elements just before it is outputted by WordPress.
Note that the filter 'script_loader_tag' will not be applied to script elements added by calling wp_localize_script() since these are outputted by the WordPress code:
echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
echo "/* <![CDATA[ */\n";
echo "$output\n";
echo "/* ]]> */\n";
echo "</script>\n";
Since these are echoed and <script type='text/javascript'> is hard coded you cannot add an attribute to the HTML script elements of wp_localize_script().
Try to use wp_enqueue_scripts hook
function my_special_inline_script() {
?>
<script>
// your JS code
</script>
<?php
}
add_action( 'wp_enqueue_scripts', 'my_special_inline_script', 1, 1 );
or
function theme_prefix_enqueue_script() {
wp_add_inline_script( 'jquery', 'jQuery(document).ready(function(){});' );
}
add_action( 'wp_enqueue_scripts', 'theme_prefix_enqueue_script' );
Bit late to the party but I just faced this exact problem for the same reasons.
I solved it for wp_add_inline_script by a bit of simple str_replace action.
add_filter( 'script_loader_tag', 'add_nonce_to_script_tag', 10, 3 );
function add_nonce_to_script_tag( $tag, $handle, $src ) {
// Check the $handle and respond accordingly
if ( $handle === 'my-script' ) {
$nonce_value = wp_create_nonce('my__script__nonce'); // or ref to an existing nonce
$replace = sprintf("javascript' nonce='%s'>", $nonce_value );
$tag = str_replace( "javascript'>", $replace, $tag);
}
return $tag;
}
// Then... $data is the inline JS from wherever
wp_add_inline_script('my-script', $data, 'before');
Once the inline script loads, I am seeing the script tag output with a nonce attribute. This is working fine with my Content-Security-Policy.
Check this out
"WordPress 5.7 adds a handful of new functions that enables passing attributes, such as async or nonce"

Move up product data woocommerce on mobile

is there a way ON MOBILE to move the 3 dropdowns section to the above the product name and gallery image?
I am using the woocommerce plugin and for the dropdowns im using the PRODUCT DATA - VAriable product section.
check the link:
Website
I would need more about your actual code (or a demo version) to achieve what you exactly want. You could use a bit of jQuery in your function.php, the following code as not been tested but the logic is there:
/*Move the variation div to another created one
======================================================================= */
add_action( 'wp_footer', 'move_variations_product_tab' );
function move_variations_product_tab() {
if (is_product()) :
$product = wc_get_product( get_the_ID() );
if($product->is_type( 'composite' )){
?>
<script>
jQuery(document).ready(function($) {
$(window).load(function() {
$( ".variations_form" ).wrap( "<div id='variations-content'></div>" );
$( ".woocommerce-product-gallery" ).before( "<div class='container product-before-gallery'></div>" );
document.getElementsByClassName("product-before-gallery")[0].appendChild(
document.getElementById("variations-content")
);
});
});
</script>
<?php
}
endif;
}
Replace "product-short" by the container of your description.
Wrapping around the div you want to move.
Creating a new class before the desired location
Move the wrapped div (#variations-content) under the newly created one
(.product-before-gallery).
Hope that helps! Let me know how it goes and give more info if you needs help.
Cheers

get_field() of ACF plugin wordpress assign to javascript value

Im having a problem of assigning data of get_field() to a javascript variable
<?php $event_message = get_field('event_message');?>
<script>
var event_message = '<?php echo $event_message; ?>';
console.log(event_message);
</script>
it gave me empty value. Am I missing something?
Your code looks fine. However, be sure to be inside the Wordpress loop or else you would have to pass the $post_id parameter to your get_field() call, like this :
<?php $event_message = get_field('event_message', $post_id); ?>
If that doesn't do the trick, we're gonna need more code to find what's wrong !
You can probably just assign the Javascript variable directly from the ACF field. So try changing your code to:
<script>
var event_message = '<?php the_field("event_message"); ?>';
console.log(event_message);
</script>
That should work, hopefully!

Renaming a default taxonomy field ('description') in WordPress

I'd like to either utilize the description field in a custom taxonomy I'm building but rename it to bio, or remove description altogether and create the new field on my own.
Any idea what I need to do either of these?
What about this jQuery hack? Place it in functions.php of your theme or create a simple plugin:
function rename_category_description() {
global $current_screen;
if ( $current_screen->id == 'edit-category' ) { ?>
<script type="text/javascript">
jQuery('document').ready(function() {
jQuery("label[for='tag-description']").text("Bio");
});
</script>
<?php }
}
add_action('admin_head', 'rename_category_description');

Resources