Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

WordPress register_activation_hook Not Working

WordPress Plugin on plugin activation I am trying to add anb ACF Fiield Group.  On plugin activation, the plugin is fired. When the plugin fires the 

if (function_exists('acf_add_local_field_group')) { is found and that check is passed.  However, the function 

 acf_add_local_field_group(array( Does Nothing.  It does not add the Field Group.


Could it be that the Location value of "careers" is stopping it because the careers post_type is a Custom Post Type?  My function is as follows:

register_activation_hook(__FILE__, 'add_career_page_field_group');


function add_career_page_field_group()

{

    //die('The add_career_page_field_group() function is being called.');

    if (function_exists('acf_add_local_field_group')) {

        //die('The acf_add_local_field_group() function is being called.');

        acf_add_local_field_group(array(

            'key' => 'group_64467f7f7820c',

            'title' => 'Career Page',

            'fields' => array(

                array(

                    'key' => 'field_64467f87c8ed6',

                    'label' => 'Apply Link',

                    'name' => 'apply_link',

                    'type' => 'url',

                    'instructions' => '',

                    'required' => 0,

                    'conditional_logic' => 0,

                    'wrapper' => array(

                        'width' => '',

                        'class' => '',

                        'id' => '',

                    ),

                    'default_value' => '',

                    'placeholder' => '',

                ),

                array(

                    'key' => 'field_6446803a2db66',

                    'label' => 'Summary',

                    'name' => 'summary',

                    'type' => 'textarea',

                    'instructions' => '',

                    'required' => 0,

                    'conditional_logic' => 0,

                    'wrapper' => array(

                        'width' => '',

                        'class' => '',

                        'id' => '',

                    ),

                    'default_value' => '',

                    'placeholder' => '',

                    'maxlength' => '',

                    'rows' => '',

                    'new_lines' => '',

                ),

            ),

            'location' => array(

                array(

                    array(

                        'param' => 'post_type',

                        'operator' => '==',

                        'value' => 'careers',

                    ),

                ),

            ),

            'menu_order' => 0,

            'position' => 'normal',

            'style' => 'default',

            'label_placement' => 'top',

            'instruction_placement' => 'label',

            'hide_on_screen' => '',

            'active' => true,

            'description' => '',

        ));

    }

}


Avatar of Noah
Noah
Flag of Singapore image

Hi!

Here is an updated code. I have included notes in the code for you reference.

<?php
/*
Plugin Name: Your Plugin Name
Plugin URI: http://your-plugin-uri.com/
Description: Plugin description here
Version: 1.0
Author: Your Name
Author URI: http://your-website.com/
License: GPL2
*/

// Register activation hook to execute code on plugin activation
register_activation_hook(__FILE__, 'add_career_page_field_group');

function add_career_page_field_group()
{
    // Check if the ACF plugin is active and the required function is available
    if (function_exists('acf_add_local_field_group')) {

        // Define an array of fields for the field group
        $fields = array(
            array(
                'key' => 'field_64467f87c8ed6',
                'label' => 'Apply Link',
                'name' => 'apply_link',
                'type' => 'url',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'default_value' => '',
                'placeholder' => '',
            ),
            array(
                'key' => 'field_6446803a2db66',
                'label' => 'Summary',
                'name' => 'summary',
                'type' => 'textarea',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'default_value' => '',
                'placeholder' => '',
                'maxlength' => '',
                'rows' => '',
                'new_lines' => '',
            ),
        );

        // Define an array of arguments for the field group
        $args = array(
            'key' => 'group_64467f7f7820c',
            'title' => 'Career Page',
            'fields' => $fields, // Use the $fields array we defined earlier
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'careers', // Set the value to your custom post type name
                    ),
                ),
            ),
            'menu_order' => 0,
            'position' => 'normal',
            'style' => 'default',
            'label_placement' => 'top',
            'instruction_placement' => 'label',
            'hide_on_screen' => '',
            'active' => true,
            'description' => '',
        );

        // Call the acf_add_local_field_group() function with the $args array
        acf_add_local_field_group($args);
    }
}
?>

Open in new window

Avatar of Robert Granlund

ASKER

I really appriciate the you commenting the  activation code that I wrote.  However, I am at a loss in decerning how it rectifies my issue?  Can you elabote?  It does not work.

When you add custom fields using code like acf_add_local_field_group, they don't persist anywhere in the database. They have to be "added" each time you want to be able to use them. 


The register_activation_hook only fires on your plugin activation, so you're basically temporarily adding them once and then they're never presented again. 


This is noted in the fine print in the Getting started section of the documentation here:

https://www.advancedcustomfields.com/resources/register-fields-via-php/

@Robert What do you mean it doesn't work? Is there a specific error that appears?

I should've noted that you can try simply running this outside of plugin activation. Just run it as a normal step in your plugin.

@all basically I want to add this field group in ACF programatically.  How do I achienve this?  This is the code I exported from the other site.  I can manually recreate it but that would defete the purpose of creating the ACF Field Group programatically.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial