Setting a cookie upon specific URL visit

I’ve used this and it works: add_action(‘init’, ‘optin_cookie’, 1); function optin_cookie(){ $currentURL = “https://” . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]; if ( $currentURL == ‘https://25dni.si/delovanje-uma/’ ) : setcookie( ‘opt_in’, has_opt_in, time()+31556926, “https://wordpress.stackexchange.com/”); elseif ( $currentURL != ‘https://25dni.si/’ ) : if ( ! isset( $_COOKIE[‘opt_in’] ) ) : endif; endif; } I’m sure there is a much more … Read more

How do I get current page ID in WordPress customizer file?

Thanks for your question. active_callback is exactly what you’re looking for. You can use it with controls: $wp_customizer->add_control( ‘setting_name’, array( ‘type’ => ‘text’, ‘section’ => ‘section_name’, ‘label’ => ‘Option with context’, ‘active_callback’ => ‘is_front_page’ ) ); and with sections: $wp_customize->add_section( ‘section-name’, array( ‘title’ => ‘Section with context’, ‘active_callback’ => ‘is_front_page’ ) ); In examples above, … Read more

Handling Body class based on Template

I’m not sure if I understand your question correctly, but… If you want to set body classes based on current page, then you can use this code function my_body_class( $classes ) { if ( is_home() ) { $classes[] = ‘wbody’; } else { $classes[] = ‘gbody’; } return $classes; } add_filter( ‘body_class’, ‘my_body_class’ ); Of … Read more

tracking number field in Woocommerce order [closed]

The syntax to get your custom field is: get_post_meta($post_id, $key, $single); In your context, you should try: $tracking_id = get_post_meta($order->get_order_number(), ‘Tracking_ID’, true); To show you in your example: <?php /* translators: %s: Site title */ $tracking_id = get_post_meta($order->get_order_number(), ‘Tracking_ID’, true); ?> <p><?php printf( esc_html__( ‘Your %s order is completed. Your tracking number is: %s’, ‘woocommerce’ … Read more

Same log message keeps on printing to debug.log file thousand of times

OK, as far as I understand, you’ve put this code: $debug_log = “DEBUG-debug log”; trigger_error($debug_log); //OR error_log($debug_log); directly in your functionsphp` file. It means, that this code will be executed (so new log item will be added to log file) every time the functions.php file is loaded – so during every request to your WP. … Read more

Passing PHP Function Arguments from CLI vs URL

You can do this: function foo_command( $args ) { WP_CLI::success( $args[0] ); } WP_CLI::add_command( ‘foo’, ‘foo_command’ ); And put it in a plugin, then run: wp foo “hello world” And it will print out hello world. Just because the built in subcommands don’t do what you need them to do, doesn’t mean you can’t add … Read more

WordPress 5 WP REST routes – No errors

REST API should always return JSON, so it’s output can be parsed as such. If it’ll return something that is not correct JSON, then your JS scripts may stop working. This is why there should be no errors printed in these requests – such messages break JSON response and make it hard to parse. And … Read more