简体   繁体   English

如何使用file_put_contents()在新的php文件中插入PHP代码?

[英]How to insert PHP code ot new php file using file_put_contents()?

I am trying to insert some PHP code to a new PHP file. 我正在尝试将一些PHP代码插入到新的PHP文件中。 To do that I am using: 为此,我正在使用:

if ( $this->fblcs_how_to_show == 1 ) {


    $insert_fb_code = <<<INSERTPHPCODE
    <?php 
    global $post;
    $fblcs_permalink    =   get_the_permalink( $post );
    echo  "<div class='fb-comments fbcls-front' data-href='$fblcs_permalink' data-width='' data-numposts='5' data-mobile='true'></div>";
    INSERTPHPCODE;

    file_put_contents( plugin_dir_path( dirname( __FILE__ ) ) . '/public/partials/extra-template/comments-template.php', $insert_fb_code );
}

Now when I open that file where I insert the code it's showing me following code: 现在,当我打开该文件并插入代码时,它向我显示以下代码:

<?php 
global $post;
    =   get_the_permalink(  );
echo  "<div class='fb-comments fbcls-front' data-href='' data-width='' data-numposts='5' data-mobile='true'></div>";

you can see that It doesn't add exactly the same content! 您会看到它没有添加完全相同的内容!

can you tell me why and how can I fix it? 您能告诉我原因以及如何解决吗?

What is happening is that it is replacing variables with their values, instead of the names, which are not defined and therefore with no value. 发生的事情是它正在用变量的值代替变量,而不是没有定义的名称,因此没有值。 What you can do is: 您可以做的是:

    if ( $this->fblcs_how_to_show == 1 ) {
        $insert_fb_code = '
            <?php 
            global $post;
            $fblcs_permalink    =   get_the_permalink( $post );
            echo  "<div class=\"fb-comments fbcls-front\" data-href=\"$fblcs_permalink\" 
data-width=\"\" data-numposts=\"5\" data-mobile=\"true\"></div>";
    ';

        file_put_contents( plugin_dir_path( dirname( __FILE__ ) ) . '/public/partials/extra-template/comments-template.php', $insert_fb_code );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM