Home > database >  WordPress Plugin: PHP Best Practice
WordPress Plugin: PHP Best Practice

Time:01-26

I have written a wordpress plugin which reads the value from an ACF field and outputs an image. The image is fetched through a shortcode.

I am a beginner and have some questions about this:

  • Is this approach heavy on performance?
  • Which solution would be best practice or more efficient?
  • Is there a possibility to optimize the code of the plugin?

For example, wouldnt it be better to set the images "globally". If an image changes, I have to change it everywhere, and that's a lot of work.

    $auswahl = get_field('dunstesse_energielabel');
    if( $auswahl == 'A'){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-A-re.png">' ;
    } else if ( $auswahl == 'B' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-B-re.png">' ;
    } else if ( $auswahl == 'C' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-C-re.png">' ;
    } else if ( $auswahl == 'D' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-D-re.png">' ;
    } else if ( $auswahl == 'E' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-E-re.png">' ;
    } else if ( $auswahl == 'F' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-F-re.png">' ;
    } else if ( $auswahl == 'G' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-G-re.png">' ;
    }              
        return $result;
}
add_shortcode('getEnergieDunstesse' , 'get_energie_dunstesse');


function get_energie_flachschirmhaube() {
    $auswahl = get_field('flachschimhaube_energielabel');
    if( $auswahl == 'A'){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-A-re.png">' ;
    } else if ( $auswahl == 'B' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-B-re.png">' ;
    } else if ( $auswahl == 'C' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-C-re.png">' ;
    } else if ( $auswahl == 'D' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-D-re.png">' ;
    } else if ( $auswahl == 'E' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-E-re.png">' ;
    } else if ( $auswahl == 'F' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-F-re.png">' ;
    } else if ( $auswahl == 'G' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-G-re.png">' ;
    }              
        return $result;
}
add_shortcode('getEnergieFlachschirmhaube' , 'get_energie_flachschirmhaube');


function get_energie_induktion() {
    $auswahl = get_field('induktions-kochfeld_energielabel');
    if( $auswahl == 'A'){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-A-re.png">' ;
    } else if ( $auswahl == 'B' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-B-re.png">' ;
    } else if ( $auswahl == 'C' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-C-re.png">' ;
    } else if ( $auswahl == 'D' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-D-re.png">' ;
    } else if ( $auswahl == 'E' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-E-re.png">' ;
    } else if ( $auswahl == 'F' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-F-re.png">' ;
    } else if ( $auswahl == 'G' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-G-re.png">' ;
    }              
        return $result;
}
add_shortcode('getEnergieInduktion' , 'get_energie_induktion');


function get_energie_glaskeramik() {
    $auswahl = get_field('glaskeramik-kochfeld_energielabel');
    if( $auswahl == 'A'){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-A-re.png">' ;
    } else if ( $auswahl == 'B' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-B-re.png">' ;
    } else if ( $auswahl == 'C' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-C-re.png">' ;
    } else if ( $auswahl == 'D' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-D-re.png">' ;
    } else if ( $auswahl == 'E' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-E-re.png">' ;
    } else if ( $auswahl == 'F' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-F-re.png">' ;
    } else if ( $auswahl == 'G' ){
        $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-G-re.png">' ;
    }              
        return $result;
}
add_shortcode('getEnergieGlaskeramik' , 'get_energie_glaskeramik');

CodePudding user response:

In the terms of the code readability you can refactor each function to this form:

function get_energie_induktion() {
    $auswahl = get_field('induktions-kochfeld_energielabel');
    $defaultValue = <your-default-value>
    $allowedValues = ['A','B','C','D','E'];

    return in_array($auswahl, $allowedValues) ? 
        '<img  src="/wp-content/uploads/EEK_Buttons_2021-'.$auswahl.'-re.png">'
        : $defaultValue;
}

And for $defaultValue you need to specify what you want to return in case no allowed option has been chosen.

Regarding the performance it doesn't really matter. You don't optimize such things over readibility, because micro-optimization is bad optimization.

CodePudding user response:

In theory your method, should not be heavy on performance as it only runs when the shortcode is present on a page or post. But, the use of the get_field() function can be slow if it is called multiple times on the same page.

You can use a switch statement rather than multiple if-else statements, this will make the code more readable and efficient. To optimize the code of the plugin you can refactor the function to use a switch statement.

function get_energie_dunstesse() {
    $auswahl = get_field('dunstesse_energielabel');
    switch ($auswahl) {
        case 'A':
            $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-A-re.png">';
            break;
        case 'B':
            $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-B-re.png">';
            break;
        case 'C':
            $result = '<img  src="/wp-content/uploads/EEK_Buttons_2021-C-re.png">';
            break;
        // and so on...
        default:
            $result = '';
    }
    return $result;
}

To avoid having to change the image everywhere, you could use a centralized approach where you store the image URLs in an array and then reference them in the function using the value of the ACF field as the key.

$images = [
    'A' => '/wp-content/uploads/EEK_Buttons_2021-A-re.png',
    'B' => '/wp-content/uploads/EEK_Buttons_2021-B-re.png',
    'C' => '/wp-content/uploads/EEK_Buttons_2021-C-re.png',
    // and so on...
];

function get_energie_dunstesse() {
    $auswahl = get_field('dunstesse_energielabel');
    if (isset($images[$auswahl])) {
        $result = '<img  src="'.$images[$auswahl].'">';
    } else {
        $result = '';
    }
    return $result;
}

You only need to update the image URL in one place then, instead of in multiple places throughout your code.

  • Related