My Button Default Click Is Not Working In WordPress Using Javascript Code

Anytime you want to use JavaScript in WordPress, you should enqueue it. Save the JS as a file, ie clickbutton.js inside your theme folder: document.getElementById(“defaultOpen”).click(); Then in functions.php, enqueue that file: add_action(‘wp_enqueue_scripts’, ‘wpse_371294_enqueue_js’); function wpse_371294_enqueue_js() { wp_enqueue_script(‘click-button’, get_template_directory_uri() . ‘/clickbutton.js’, array(”), “1.0”, true); } (If you only need the file to run on one page, … Read more

Change background image based on the hour [closed]

This question isn’t WordPress specific so is perhaps better for StackOverflow instead. We can make it WordPress specific by suggesting the use of wp_enqueue_script to add the script to the page, but ultimately this is a JavaScript question. I’m basing my answer off of this article // Get the local time of the visitor var … Read more

How to generate the COOKIEHASH from JavaScript

By default it’s the MD5 hash of the site URL: $siteurl = get_site_option( ‘siteurl’ ); if ( $siteurl ) { define( ‘COOKIEHASH’, md5( $siteurl ) ); } This includes the scheme but not a trailing slash, so in your example you’d need the hash of https://zeth.dk. You can also override this by defining COOKIEHASH in … Read more

useSelect second parameter

Short Answer The second parameter for useSelect is the second parameter for useCallback, so check the React website and you’d understand what the heck that second parameter is.. 🙂 Long Answer useSelect is a custom React hook which uses the core useCallback hook in React which returns a memoized callback which can avoid expensive calculations … Read more

How to display post content in the block editor

It is true that using RawHTML (or dangerouslySetInnerHTML) is discouraged and indeed prone to XSS attacks like <img src=x onerror=alert(1)//> and <p>abc<iframe//src=jAva&Tab;script:alert(3)>def</p>. But if you need to set HTML directly from React, then you’ll have to use dangerouslySetInnerHTML, either directly (e.g. <div dangerouslySetInnerHTML={ ({ __html: ‘foo <b>bar</b>’ }) } />) or via RawHTML in Gutenberg … Read more

Looping over wordpress meta to create “ ‘s?

Does select(“core/editor”).getEditedPostAttribute(“meta”) get the whole post meta? Only the meta registered using register_post_meta() or register_meta() with the show_in_rest argument set to true. So for example, the meta can be registered like so: register_post_meta( ‘post’, ‘_postcode_pricing_postal_town’, array( ‘show_in_rest’ => true, ‘single’ => true, ‘type’ => ‘string’, // I set this because the meta is *protected*, i.e. … Read more

is_page_template() written in jquery/javascript

Well, yes, in WordPress, different templates have a different body class name. If you inspect the body classes on a specific page in WordPress, there should be a body class page. You can detect the template by checking if the class exists like so: jQuery(document).ready(function($){ if ( $(‘body’).hasClass(‘page’)) { // add your logic here } … Read more

Display a div when post has a certain tag

You could use a simple conditional and check if the post has a term using has_term(): has_term( $term, $taxonomy, $post ); Pass in your Term Slug, the taxonomy ( which sounds like you’re using post_tag and the post object, no javascript needed. <?php if( has_term( ‘term_slug’, ‘post_tag’, $post ) : ?> <div> Show your div … Read more