Ask questions about WordPress courses

4 useful code snippet

Adding Image Sizes to the Media Library

Using the code, you can show the size of the pictures in a separate column. It is very easy to understand, how to add column in wordpress admin.

function wh_column( $cols ) {
        $cols["dimensions"] = "Dimensions (w, h)";
        return $cols;
}
function wh_value( $column_name, $id ) {
    $meta = wp_get_attachment_metadata($id);
           if(isset($meta['width']))
           echo $meta['width'].' x '.$meta['height'];
}
add_filter( 'manage_media_columns', 'wh_column' );
add_action( 'manage_media_custom_column', 'wh_value', 10, 2 );

Displaying information about a topic

To display title, author, links to the author's page, version..., you can use the code.

<?php
    $theme_data = get_theme_data(get_stylesheet_uri());
    echo $theme_data['Title'];
    echo $theme_data['Author'];
?>

 

Google Chart API embed via shortcode.

Adding code to a file functions.php:

function google_chart_shortcode( $atts ) {
        extract(shortcode_atts(array(
            'data' => '',
            'size' => '400x180',
            'colors' => '',
            'title' => '',
            'labels' => '',
            'type' => 'pie',
            'advanced' => ''
        ), $atts));
        switch ($type) {
                case 'line' :
                        $charttype = 'lc'; break;
                case 'xyline' :
                        $charttype = 'lxy'; break;
                case 'sparkline' :
                        $charttype = 'ls'; break;
                case 'meter' :
                        $charttype = 'gom'; break;
                case 'scatter' :
                        $charttype = 's'; break;
                case 'venn' :
                        $charttype = 'v'; break;
                case 'pie' :
                        $charttype = 'p3'; break;
                case 'pie2d' :
                        $charttype = 'p'; break;
                default :
                        $charttype = $type;
                break;
        }
        if ($title) $string .= '&chtt='.$title.'';
        if ($labels) $string .= '&chl='.$labels.'';
        if ($colors) $string .= '&chco='.$colors.'';
        $string .= '&chs='.$size.'';
        $string .= '&chd=t:'.$data.'';
        return '<img title="'.$title.'" src="https://chart.apis.google.com/chart?cht='.$charttype.''. $string.$advanced.'" alt="'.$title.'" />';
}
add_shortcode('chart', 'google_chart_shortcode');

Once added, you can use the shortcode to place google charts in your articles.

Short code example:

[chart data="41,37.89,10.67,2.03" labels="Reffering+sites|Search+Engines|Direct+traffic|Other"  colors="ff0000,005599,fff000,000000" size="488x200" title="Traffic  Sources" type="pie"]

 

We display the signature under the registration form

For those who register, you can make a greeting or explanation. Make it easy, include code in functions.php file.

add_action('register_form', 'register_message');
function register_message() {
    $html = '
        <div style="margin:10px 0;border:1px solid #e5e5e5;padding:10px">
            <p style="margin:5px 0;">
            Joining this site you agree to the following terms. Do no harm!
            </p>
        </div>';
    echo $html;
}

Good luck with development!


Купить хостинг WordPress
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal