0

I'd like to better understand - when I place the below into my functions.php file:

    add_action('customize_register', 'boat_annualReport_customize_register');
   function boat_annualReport_customize_register($wp_customize) {
      
      $wp_customize->add_section('boat-annualreport-callout', array ('title' => 'Annual Reports',
      // 'priority' => 1,
      //  'default' => 'testing',
       'title' => __('Annual Report', 'boat')
   ));

   $wp_customize->add_setting('boat-annualreport-callout-headline', array(
      'default' => 'Annual Report'
      ));
      
      $wp_customize->add_control( new WP_Customize_Control($wp_customize, 'boat-annualreport-callout-headline-control', array (
      'label' => 'Text',
      'section' => 'boat-annualreport-callout',
      'settings' => 'boat-annualreport-callout-headline',
      'type' => 'textarea',
      // 'active_callback' => 'boat_annualreport_callout_image_on_condition',
      )));

      $wp_customize->add_setting('boat-annualreport-callout-image', array(
      'type' => 'theme_mod', // or 'option'
      'capability' => 'edit_theme_options',
      'transport' => 'refresh',
      'sanitize_callback' => '',
      'sanitize_js_callback' => '' // Basically to_json.
      ));
      
      $wp_customize->add_control( new WP_Customize_Cropped_Image_Control($wp_customize, 'boat-annualreport-callout-image', array (
      'label' => 'Hero Image',
      'section' => 'boat-annualreport-callout',
      'settings' => 'boat-annualreport-callout-image',
      'type' => 'image',
      // 'active_callback' => 'boat_annualreport_callout_image_on_condition',
      'height' => 590,
      'width' => 1920
      )));
   }

It loads in the wp-admin customize section perfectly.

But when I place the code above from my functions.php file into my annualReportFunctions.php file and then modify my functions.php to the code below it does not load the custom_register function in the annualReportFunctions.php file. It does open the annualReportFunctions.php file, and it will fire other functions in it - but it will not fire any custom_register functions.

Please help me understand this. My functions.php file is going to be huge if I have to include every pages functions into one file - I would like to be able to split them up to load as needed :/

function boatTheBusForOutdoorAccess_register_scripts() {

   global $stringTmp;
   global $themeIncPath;
   echo $stringTmp;

   if ( $stringTmp === "" ) {
      require_once dirname( __FILE__ ) . '/inc/homeFunctions.php';
      // boat_homehero_customize_register($wp_customize);

   } elseif ( $stringTmp === 'find-your-trip' ) {
      require_once dirname( __FILE__ ) . '/inc/findYourTripFunctions.php';
      
      } elseif ( $stringTmp === 'who-we-are' ) {
         require_once dirname( __FILE__ ) . '/inc/whoWeAreFunctions.php';
      
      } elseif ( $stringTmp === 'what-we-do' ) {
         require_once dirname( __FILE__ ) . '/inc/whatWeDoFunctions.php';
      
      } elseif ( $stringTmp === 'support-our-mission' ) {
         require_once dirname( __FILE__ ) . '/inc/supportOurMissionFunctions.php';
      
      } elseif ( $stringTmp === 'plan-a-trip' ) {
         require_once dirname( __FILE__ ) . '/inc/planATripFunctions.php';
      
      } elseif ( $stringTmp === 'annual-report' ) {
         echo " 1";
         // global $stringTmp;
         require_once dirname( __FILE__ ) . '/inc/annualReportFunctions.php';
         echo " 4";      
      } else {
         echo "Page template slug is not an array or is not set.";
      }
   }

   
function boatTheBusForOutdoorAccess_register_url() {
   global $url;
   global $path;
   global $stringTmp;
   global $wp;

   if (isset($url) && is_array($url)) {
      echo " First hooray. This is an array and it is set ";

      $stringTmp = implode($url);
      $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
      $stringTmp = basename($path, ".php");  // Will remove `.php` suffix
   }
   boatTheBusForOutdoorAccess_register_scripts();
}
add_action( 'wp_enqueue_scripts',  'boatTheBusForOutdoorAccess_register_url');

3
  • What calls boatTheBusForOutdoorAccess_register_url()? When is it called? Is it called after the customize_register action has been fired?
    – Rup
    Nov 11, 2023 at 17:21
  • @Rup ah it got cut off but I have it being called right under: add_action( 'wp_enqueue_scripts', 'boatTheBusForOutdoorAccess_register_url'); but since you mentioned that is probably the issue and not the way best way to call it so it fires in order......will test some stuff. Thank you so much!!
    – Dreamyth
    Nov 11, 2023 at 17:30
  • IIRC wp_enqueue_scripts isn't used on the admin site. Try calling it for admin_enqueue_scripts too.
    – Rup
    Nov 11, 2023 at 20:38

1 Answer 1

0

In the second version you're running your code as a wp_enqueue_scripts hook. This is for frontend scripts and isn't called in the admin site. For that you need to hook admin_enqueue_scripts as well:

add_action( 'wp_enqueue_scripts', 'boatTheBusForOutdoorAccess_register_url');
add_action( 'admin_enqueue_scripts', 'boatTheBusForOutdoorAccess_register_url');

That said, it might be simpler to just include your admin scripts based on is_admin()

if ( is_admin() && ! wp_doing_ajax() ) {
    require_once dirname( __FILE__ ) . '/inc/annualReportFunctions.php';
}

It looks like you're trying to only load page-specific PHP on the right pages: I don't know how much that would really help (I think modern PHP runtimes have opcode caches etc.) but for the admin site at least I don't think it's worth worrying about and you might as well load all admin functions always there.

(Also note that you can use __DIR__ for the path: dirname( __FILE__ ) is for compatibility with old PHPs that WordPress no longer supports anyway.)

1
  • You are WONDERFUL @Rup - this was the most comprehensive answer I've gotten, THANK YOU!!!
    – Dreamyth
    Nov 12, 2023 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.