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);
}
谁能指出我做错了什么?任何帮助,将不胜感激。
解决方案
推荐阅读
- xaml - UWP XAML 智能感知 DataTemplate.DataType
- .net - .NET Profiler - 获取函数参数的类型
- typescript - 基于 Key 值在 TypeScript 中创建动态记录类型
- pandas - 如何提高 pyspark 到 Pandas 数据帧转换的效率,而不是 PyArrow 或使用它
- python - 创建工厂类
- python - 当我在 sqlalchemy 查询中使用方括号时使用的查询是什么?
- spring - Spring Boot CORS 不适用于 React 应用程序
- sql - Postgres:从值> = x以及没有值大于x的对象数组中选择所有行
- angular - 需要根据路线在我的标题中有不同的按钮
- python-3.x - 从产品描述中提取数字数据