首页 > 解决方案 > 将函数调用到来自另一个模板文件 WordPress 的另一个函数中的变量?

问题描述

所以我正在使用 WordPress 定制器,我需要用户在定制器中编辑他们的 API 密钥。问题是每个函数都需要从单独的模板文件中操作。我需要一个函数内的变量来调用另一个模板文件中的函数,该函数可以在它周围产生单引号''。

硬编码的工作原理:


//api.php

function api() {
//this is a random API key
 $api_key = '000000000000000000';
}


//customizer.php
//This is what I need to call to $api_key. It actually doesn't do anything on its own 

function newsletter_api_key($newsletter_api) {
  $newsletter_api = get_option('newsletter_api_key_setting', '');
  return $newsletter_api;
}
add_action('wp_head', 'newsletter_api_key');

function customizer_newsletter($wp_customize) {
  $wp_customize->add_panel('newsletter_key_panel', array(
    'priority'      => 120,
    'title'         => esc_html__('Newsletter Settings', 'testWP'),
  ));
  /*
  Section for Api & ID
  */
  $wp_customize->add_section('newsletter_key_section', array (
    'title'       => esc_html__('MailChimp API & ID Key', 'testWP'),
    'description' => esc_html__('Enter you Api and ID to start receiving subscribers', 'testWP'),
    'priority'    => 1,
    'panel'       => 'newsletter_key_panel',
  ));
  //API setting / control
  $wp_customize->add_setting('newsletter_api_key_setting', array(
    'default'           => '',
    'capability'        => 'edit_theme_options',
    'transport'         => 'refresh',
    'type'              => 'option',
    'sanitize_callback' => 'wp_filter_nohtml_kses',
  ));

  $wp_customize->add_control('newsletter_api_key_control', array(
    'settings'  => 'newsletter_api_key_setting',
    'label'     => esc_html__('API Key', 'testWP'),
    'section'   => 'newsletter_key_section',
    'type'      => 'text',
    'description' => esc_html__('Enter your API key here.', 'testWP'),
  ));

}
add_action('customize_register', 'customizer_newsletter');

我尝试了什么:

function api() {
//this is a random API key
 $api_key = newsletter_api_key($newsletter_api);
}

谁能指出我做错了什么?任何帮助,将不胜感激。

标签: phpwordpress

解决方案


推荐阅读