Checking the count within a foreach loop

You can achieve this very easily, just count the array with the terms. Also if I could suggest something more, I’d check for WP_Error a bit earlier, outside the foreach loop, instead of checking every single term, especially that you’re not using the $term_link variable anywhere. Just checking if get_the_terms() returns an array should be … Read more

JavaScript in a PHP plugin

So the question and answer you linked to is still valid, the only part that changes is the AJAX part. Specifically, there are now 2 options: Admin AJAX API: A legacy API, most tutorials will use this, but it’s much harder to secure, and provides little to no help. If it isn’t implemented right you … Read more

Separation of presentation and code – WordPress

TLDR; Separate your view and business logic, or don’t – WordPress doesn’t care either way. Additional template engines aren’t necessary. Start small developing with vanilla PHP & WordPress; just build something that works, then separate it’s view and business logic. Continue refactoring as you learn and the project grows. When you’re comfortable with theme and … Read more

Single Quotes in .php file doesn’t load website

Your input line is enclosed in single quotes, so when PHP sees the single quote inside that string, it thinks you’re done with the string and jumping back into PHP. The way to fix this is to escape quotes inside the string so the single quoted words inside ga() change from ‘send’ to \’send\’.

How to get post ID in a Page?

You should not use globals directly. There are some nice wrapper functions in the WordPress API. If you want to retrieve the ID of the current post or page just use get_the_ID(). Without any test … you could write the functionality in this way: $file_id = get_post_meta( get_the_ID(), ‘sound_s’, true ); $file_url = wp_get_attachment_url( $file_id … Read more

Read array in php?

Fastest way would be something like this: $fruit_id = get_user_meta($user_id, ‘fruit’, true); //returns 1 $display_fruit = some_fruits($fruit_id); //will return ‘Apple’

Printing HTML codes

If I understand your question well, your result shows your <hr> in text form. When you’re using esc_html__( string $text, string $domain = ‘default’ ), the returned output is escaped, it means : ” is replaced with &quot; & is replaced with &amp; < is replaced with &lt; > is replaced with &gt; So your … Read more

Extend Woocommerce Widget Class – WC_Widget_Product_Categories

When calling parent::__construct() from WC_Widget_Product_Departments the constructor of WC_Widget_Product_Categories immediately overwrites the class properties (especially the widget_id), see here at GitHub. So you have to extend WC_Widget to create a new widget and then you can copy anything what you need in your widget from WC_Widget_Product_Categories to WC_Widget_Product_Departments.