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

How to update WP about renamed files

Here is the code that works. Files are moved to ‘username’ folder and names for all files (including width x height) are updated. // Get all uploaded file attachment IDs $media_ids = $_POST[‘item_meta’][205]; // file upload field ID // Loop through each attachment foreach ( (array) $media_ids as $id ) { if ( ! $id … Read more

How to get the Page Number of the Current Page in Single Post Pagination

For your single page, you could try using the global $page variable. Another alternative would be to try fetching the query page variable, with e.g.: $wpse_current_page = (int) get_query_var( ‘page’ ); if( $wpse_current_page > 0 ) { printf( __( ‘Page Number: %d’, ‘wpse-theme-slug’ ), (int) $wpse_current_page ); } on your single page.

multiple if statements [closed]

Each of your if statements have the same has_term(‘NBS_CONS’, ‘broad_type_nbs’ ) function. This could be put in a variable for reuse instead. & Your argument for each if statement is actually very consistent, so much so you could probably reduce it by just putting your varying data in an array, loop though, and use Variable … Read more

White Screen of Death – wp-admin

I’d comment on Fayaz’s answer, but I don’t have enough reputation to comment. So here’s my additional advice: Along with checking hidden white space characters in wp-config.php file and elsewhere, I’d recommend you to temporarily disable all plugins and switch to the default WordPress Theme (e.g. Twenty Seventeen Theme). Then check if you still have … Read more

Convert all images to PNG on file upload

(Updated answer) The sight() function not only uploads the image, but also creates an attachment (i.e. a post of the attachment type); hence, I updated the code — it’s now in a function named attachment_to_png, and that it uses the get_attached_file() to get the image file attached to that attachment, and the update_attached_file() to update … Read more

Use ajax from function.php

As Milo already said in comment, jQuery is in noConflict mode in WordPress. This way other JS libraries can also use $ character… One way to solve it is using jQuery everywhere in your JS files. Another is to create block and define $ variable in it: jQuery(function ($) { $.ajax(…); // here you can … Read more