调用一个函数到另一个模板文件WordPress中另一个函数内的变量?

时间:2019-10-04 04:05:10

标签: php 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);
}

有人可以指出我在做什么错吗?任何帮助将不胜感激。

0 个答案:

没有答案