Beautify WordPress HTML output without any plugins

Great question, I asked it myself and here’s what I came up with. You can just use this in a custom plugin or in functions.php. // turn on Output Buffering (hence *ob*) ob_start(); // register a callback to run after WordPress has outputed everything add_action(‘shutdown’, function () { // get the output buffer and store … Read more

Add HTML Snippet only on specific pages

You can try using the wp_head hook like this and checking if it’s a post or page you want it to load on with an if statement: function wse_add_snippet_to_header() { //replace the array with your own post ids that you want this on //replace is_single with is_page if you want to check for pages instead … Read more

Add HTML to single post tag

I found the best solution was to add a custom field, head, and then build a short plugin to add the contents to the head tag: add_action(‘wp_head’, ‘add_HTML_head’); function add_HTML_head(){ global $post; if(!empty($post)){ // get custom field for ‘head’ $headHTML = get_post_meta($post->ID, ‘head’, true); if(!empty($headHTML)){ echo $headHTML; } } } I’ve packaged it up into … Read more

Thumbnail + Excerpt = loss in word count

For everybody that is looking for a great excerpt that keep html tags in tact and want the excerpt not to cut off mid sentence and have a true word count, here is the code function pietergoosen_custom_wp_trim_excerpt($text) { global $post; $raw_excerpt = $text; if ( ” == $text ) { $text = get_the_content(”); $text = … Read more

Remove Line Breaks From Cell Content in WordPress

The <br/> tags are added by wpautop function that is hooked into the_content filter. So you need to unhook it from the page where your table is displayed. There are several ways to do this. With PHP you can do something like this (change the ‘your_page/post_id’ with the actual ID) function wpdg_91929_remove_autop_for_page( $content ) { … Read more

Localizing a string with html tag with word inside that tag

Possible solution to get everything translatable as one sting. <?php printf(__(‘Click <a href=”https://wordpress.stackexchange.com/questions/135866/%s”>Here</a> to go and wacth the video.’, ‘pietergoosen’), esc_url( get_permalink() )); ?> It is usually safe to assume that the translator have enough knowledge of HTML to understand what is going on there.

Custom title widget / HTML encoding

As you know, html tags doesn’t work on widget title. But there’s work around to use it. The best approach i know is to use shortcode in title. So, for using br and span, the following is a solution – add_filter(‘widget_title’, ‘do_shortcode’); add_shortcode(‘br’, ‘wpse_shortcode_br’); function wpse_shortcode_br( $attr ){ return ‘<br />’; } add_shortcode(‘span’, ‘wpse_shortcode_span’); function … Read more

How to Embed HTML files directly into WordPress

It would really help to know exactly what you’re trying to achieve. The more you work with WordPress, the more the sentence “Necessity is the mother of invention” makes sense. A WordPress site is essentially snippets of html code put together with php. For example, header.php contains the instructions to build the header, the page … Read more

Preserve white-space in Page

You are not wrapping the code inside <code> tags. <pre> <code> selector { property: value; property: value; } </code> </pre> Furthermore, most syntax highlighting plugins also use this format (both <pre> and <code>) to render code. Here you have more about this. https://css-tricks.com/snippets/css/make-pre-text-wrap/