Function to insert missing image size attributes into img tags

function replaceImageMeta( $imagename ) { preg_match_all(‘/(alt|title|src)=(“[^”]*”)/i’,$imagename, $img);//var_dump( str_replace(“\””, “” , ( $img[2][0] ) ) ); list($width, $height, $type, $attr) = getimagesize( str_replace(“\””, “” , ( $img[2][0] ) )); $imgname = str_replace(“\””, “” , ( $img[2][0] ) ); return sprintf(‘<img src=”https://wordpress.stackexchange.com/questions/235051/%s” width=”%dpx” height=”%dpx” >’, str_replace(“\””, “” , ( $img[2][0] ) ), $width, $height); } So call … Read more

Insert Content Before div#main from the functions.php File

The markup surrounding the content is not specified by WordPress, so there is no hook for that. But you can declare your own hook there. In your template file, write … <?php do_action( ‘before_content_container’ ); ?> <div class=”main”> <?php the_content(); ?> </div> <?php do_action( ‘after_content_container’ ); ?> … and then you can register a callback … Read more

Where to store credentials used in a function? [duplicate]

You could store it as variables $account_id = ‘your_account_id’; $password = ‘your password’; someFunction($account_id, $password); Or store it in {$table_prefix}_options table and retrieve it by get_option function $account_id = get_option(‘account_id’); $password = get_option(‘password’); someFunction($account_id, $password); You could save this values, for example from admin panel, by update_option function. Second option is better, because you can … Read more

Remove attachment page for audio media type only

You must use this function wp_check_filetype to check what is your media type $filetype = wp_check_filetype(‘image.jpg’); echo $filetype[‘ext’]; // will output jpg then you can have something like this $file_url = wp_get_attachment_url( $file_id ); $filetype = wp_check_filetype( $file_url ); switch ($filetype) { case ‘image/jpeg’: case ‘image/png’: case ‘image/gif’: return // do whatever you want break; … Read more

Is it possible to use add_filter in an included file in the child theme’s functions.php?

What you’re trying to do is possible. When including php files, use get_stylesheet_directory() in child themes and get_template_directory() in parent themes. E.g. require_once( get_stylesheet_directory() . ‘/inc/myfile.php’); get_stylesheet_directory_uri() and get_template_directory_uri() will return URLs. Those functions are useful when enqueuing public assets such as stylesheets and JavaScript. Also, setting allow_url_include to Off in your php.ini will provide … Read more

Protect get_query_var from manual input in url

When someone messes up an URL it’s typically not something to do anything about. If you look at any site’s log they receive tons of completely broken and malformed requests every day. You only have to care about one case — that URL parses properly and input data is valid and safe. Whenever request is … Read more

How to pass variables from one function to another or combine functions

There are 2 ways that would be the most simple. Do it with a function call. Make the variables global. The first would be done like so: function my_func() { $styles=”stuff”; second_func($styles); } function second_func($styles) { // do something with $styles var_dump($styles); } The second way would be done like so: // Set global variable … Read more

Changing where my author box is printed

You could change the function to echo like this: echo apply_filters(‘the_content’, $content); and then in your template, right before the comments template, place the call to your author box function like this: author_info_box(); also, you would need to remove the add_action(‘the_content’, ‘author_info_box’); Full function example: function author_info_box() { global $post; // Detect if it is … Read more