Woocommerce product download URL

<?php $downloads = $product->get_files(); foreach( $downloads as $key => $download ) { echo ‘<p class=”download-link”><a href=”‘ . esc_url( $download[‘file’] ) . ‘”>’ . $download[‘name’] . ‘</a></p>’; } ?> Check this.. Worked for me.

PHP Date display as time ago

There is a function for that: human_time_diff(). Use it like so: (U because we want the unix timestamp) <?php echo human_time_diff( get_the_time(‘U’) ); ?> If you want additional text, you can use the following <?php printf (esc_html__(‘%s ago’, ‘yourlanguageslug’), human_time_diff(get_the_time(‘U’))); ?>

is_page “range” for if statement?

I think you should create range as variable and set it to is_page() something like this should work. $range = range(310, 326); if(is_page($range)){ // your code.. } is_page() function can accept array as argument. And range() function return array. This example works fine for me.

How to sanitize my cookie name

Assuming $from_page is a string value and not an array or object, sanitize_key() should do the trick, it allows only a-z0-9_- and I believe is used for permalink. $cookiename = sanitize_key( ‘unrestrict_’.$from_page ); setcookie( cookiename, 1 … there’s a whole bunch of wordpress sanitizing functions, reference available in the docs.

Remove query string specific key value

[EDIT] This should work the way you wanted it to: $marca = get_query_var(‘marca’); $marca_arg = isset($marca) && is_array($marca) ? $marca : []; $url_base = remove_query_arg( ‘marca’ ); $n = count( $marca_arg ); foreach ($marcas_terms as $marca_term) { $selected = in_array( $marca_term->slug, $marca_arg ); if ( $selected ) { $marca_arg2 = array_diff( $marca_arg, [ $marca_term->slug ] … Read more

How to restrict page access to logged-in users?

How about this? function my_force_login() { global $post; if (!is_single()) return; $restricted_parent_id = 1 // Whatever id you are targeting. if ($post->post_parent == $restricted_parent_id && !is_user_logged_in()) { auth_redirect(); } } More info on post_parent and other conditional tags here: https://codex.wordpress.org/Conditional_Tags

A good strategy to print custom posts (offer) that are checked inside the metabox of a post?

The feature is achieved in this way! function op_register_menu_meta_box() { add_meta_box( ‘op-menu-meta-box-id’, // Metabox ID esc_html__( ‘Custom offers Checklist (select any 2)’, ‘text-domain’ ), //Metabox Title ‘op_render_menu_meta_box’, // Metabox Callback Function ‘listing’ // Place Where this Metbax Should Appear ); } add_action( ‘add_meta_boxes_listing’, ‘op_register_menu_meta_box’ ); //Metabox Hook The callback function! function op_render_menu_meta_box( $post ) { … Read more

Custom Template 404 for specific custom post type

This is going to track only post_type query var, but you also can add checks for query_vars of all your post_types or only ones you would like to get a custom 404 page for. add_filter( ‘404_template_hierarchy’, function( $templates ) { global $wp_query; if ( ” !== $wp_query->get(‘post_type’) ) { array_unshift( $templates, sprintf( ‘%s-404.php’, $wp_query->get(‘post_type’) ) … Read more

Extract subdomain and relative address from a url

I encountered a similar requirement, and could not find a ready-made solution for this, so I created a function that is based on the standard PHP function parse_url() and added to this over time to extract everything that I could think of. Below is my code and two examples of the output. This will extract … Read more

Escape post image attachments added to template

Running the output through escaping function should be just fine. You can either use wp_kses_post(), which by default allows the same html attributes that you would use in the post content (see in codex): echo wp_kses_post( wp_get_attachment_image( $image_id ) ); or if you want to be more precise and strict, you can pass an array … Read more