Home > Blockchain >  Create a simple plugin to show ACF in wordpress posts (if they exists)
Create a simple plugin to show ACF in wordpress posts (if they exists)

Time:01-18

After installed ACF plugin in wordpress website, and configured some custom fields, I'd like to show ACF custom fields only if they exists in my posts (articles). I followed the guide but can't find how to do it without using a child theme. I need to create an action performing even if the theme will be updated without losing my code. I'd like to have a simple plugin who can add my code below to my posts. I am not very familiar with php yet. This is my code that I inserted in a single-template:

<div id="scheda-dettagli-vino" >
                    <table>
          <tr>
                        <td>
                            <?php if (get_field('produttore'))
                            { echo '<img src="/downloads/area-di-produzione.png" />' , '<h3 style="display:inline; ">Area di produzione</h3>';}?>
                        </td>
                                    <td>
                                        <?php if(get_field('produttore'))
                                            {   echo the_field('produttore');}?>
                                        </td>
                                    </tr>

          </table>
                        </div>

CodePudding user response:

At first, you need to create a folder in the plugin directory and create a file in which the extension must be folder-name.php.

I share a sample code you just paste it. After active plugin. Hopefully, work it.

Example Folder Name: acf-helper

Example Plugin: acf-helper.php

<?php

/**
 * Plugin Name: ACF-Helper
 * Plugin URI: #
 * Description: Custom plugin for ACF.
 * Author: Monzur Alam
 * Version: 1.0.0
 * Text Domain: acf-helper
 * License: GPLv2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

if (!function_exists('acf_helper_content')) {
    function acf_helper_content( $content ){
        if(is_singular('post')){
            ob_start();
            ?>
            <div id="scheda-dettagli-vino" >
                <table>
                    <tr>
                        <td>
                            <?php 
                                if (get_field('produttore')) {
                                    echo '<img src="/downloads/area-di-produzione.png" />', '<h3 style="display:inline; ">Area di produzione</h3>';
                                } 
                            ?>
                        </td>
                        <td>
                            <?php 
                                if (get_field('produttore')) {
                                    echo esc_html(the_field('produttore'));
                                }
                            ?>
                        </td>
                    </tr>

                </table>
            </div>
            <?php
            return $content . ob_get_clean();
        }
    }
    add_filter('the_content', 'acf_helper_content');
}

enter image description here

  •  Tags:  
  • Related