dqj29136 2016-06-08 09:36
浏览 143

将php文件插入Wordpress短代码

I have had a customs script made to output a certain phone number depending on where a user has come from. I have been able to set this up within the header of a website using the simple snippet of:

<?php include 'TelephoneNumber.php';?>

I now need to create a shortcode so that I can call this within the Wordpress test editor. I have created a very simple shortcode but I cannot work out how to include the filename above.

custom_shortcodes.php (placed in the wp-content folder)

<?php
function telephone_number(){
return 'PHONE NUMBER HERE';
} // End telephone_number()
?>

function.php (themes)

include(WP_CONTENT_DIR . '/custom_shortcodes.php');
add_shortcode( 'telephone_number_sc', 'telephone_number' );

At the minute the shortcode does output the text PHONE NUMBER HERE. However, this is useless as it's not calling the script I need.

How can I use the include file function?

  • 写回答

1条回答 默认 最新

  • doujiu7704 2016-06-08 09:53
    关注

    You can either have the function in the same file as the shortcode or you can have them in separate files, but they will need to be structured correctly. Include works the same way as require, I just prefer require.

    In the same file

    In functions.php

    you need to require/include the shortcode file:

    require('custom_shortcodes.php');
    

    In custom_shortcodes.php

    // phone number function
    function telephone_number(){
            return 'PHONE NUMBER HERE';
    } // End telephone_number()
    

    To make shortcode, you need to follow a specific way:

    //shortcode
    function output_shorcode($atts, $content = null){
         $number = telephone_number();
         return $number;
    }
    add_shortcode( 'number', 'output_shortcode' );
    

    and put [number] where ever you need to have the number

    In separate files

    In functions.php

    you need to require/include the shortcode file and the number file:

    require('phone_number.php');
    require('custom_shortcodes.php');
    

    In custom_shortcodes.php

    function output_shorcode($atts, $content = null){
             $number = telephone_number();
             return $number;
        }
        add_shortcode( 'number', 'output_shortcode' );
    

    In phone_number.php

    // phone number function
        function telephone_number(){
                return 'PHONE NUMBER HERE';
        } // End telephone_number()
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型