WP仪表板中显示的页面代码

时间:2017-06-20 作者:WouterS

我在这里找不到这个问题,尽管我仔细看了看。如果是这样,我道歉。

我已经为我的WP做了一个短代码。当我将短代码放在一个页面上时,代码也可以在仪表板的页面编辑器顶部看到。

我如何防止这种情况?我的代码有没有做错什么?代码见下文。enter image description here

if ( ! function_exists( \'sollicitatie_formulier\' ) ) {
        function sollicitatie_formulier(){
            global $wpdb;
            if(isset($_POST[\'btn-upload\']))
            { 
            $table = tbl_uploads;
            $file = rand(1000,100000)."-".$_FILES[\'afile\'][\'name\'];
            $file_size = $_FILES[\'afile\'][\'size\'];
            $file_type = $_FILES[\'afile\'][\'type\'];
            $file_loc = $_FILES[\'afile\'][\'tmp_name\'];
            $folder="wp-content/themes/jobify-extended/uploads/";
            $data = array(
                \'voornaam\' => $_POST[\'avoornaam\'],
                \'familienaam\' => $_POST[\'afamilienaam\'],
                \'comment\' => $_POST[\'amsg\'],
                \'afile\' => rand(1000,100000)."-".$_FILES[\'afile\'][\'name\'],
                \'size\' => $_FILES[\'afile\'][\'size\'],
                \'type\' => $_FILES[\'afile\'][\'type\'],



            );

            // new file size in KB
            $new_size = $file_size/1024;  
            // new file size in KB

            // make file name in lower case
            $new_file_name = strtolower($file);
            // make file name in lower case

            $final_file=str_replace(\' \',\'-\',$new_file_name);

            $format = array(
                \'%s\',
                \'%s\'
            );

            if(move_uploaded_file($file_loc,$folder.$final_file)) 
            {
                $success=$wpdb->insert( $table, $data, $format,$folder);
                if($success){
                ?>
                    <script>
                    alert(\'successfully uploaded\');
                    window.location.href=\'sollicitatie-formulier?success\';
                    </script>
    <?php

}
}
}


else   {
    ?>

        <form action="" method="post" enctype="multipart/form-data">
        <label for="voornaam">Voornaam: </label><input type="text" name="avoornaam" required="">
        <label for="familienaam">Familienaam: </label><input type="text" name="afamilienaam" required="">
        <label for="comment">bericht: </label><textarea type="text" name="amsg"></textarea>
        <input type="file" name="afile" accept=".pdf, .doc, .docx" />
        <input type="submit" name="btn-upload" value="upload"</input>
        </form>

    <?php
        if(isset($_GET[\'success\']))
        {
    ?>
        <label>Kandidaat succesvol toegevoegd...  <a href="/bekijk/">click hier om kandidaten te bekijken.</a></label>
        <?php
    }
    else if(isset($_GET[\'fail\']))
    {
        ?>
        <label>Oeps...een probleem! Probeer opnieuw</label>
        <?php
    }
    else
    {
        ?>
        <label>Try to upload any files(PDF, DOC, DOCX)</label>

 <?php } }

        }
    }

    add_shortcode( \'sollicitatie_formulier\', \'sollicitatie_formulier\' );

1 个回复
最合适的回答,由SO网友:dbeja 整理而成

短代码不应回显任何输出。它们应始终返回用于替换短代码的文本。

您可以在变量上编写html代码:

function my_shortcode() {
    $output = \'<div>something</div>\';
    return $output;
}
也可以使用php输出缓冲:

function my_shortcode() {
    ob_start(); ?> 
    <div>something</div> 
    <?php 
    return ob_get_clean();
}
在您的情况下,可以将ob\\u start放在函数的开头,最后只返回ob\\u get\\u clean:

if ( ! function_exists( \'sollicitatie_formulier\' ) ) {
        function sollicitatie_formulier(){
            global $wpdb;

            ob_start();

            if(isset($_POST[\'btn-upload\']))
            { 
            $table = tbl_uploads;
            $file = rand(1000,100000)."-".$_FILES[\'afile\'][\'name\'];
            $file_size = $_FILES[\'afile\'][\'size\'];
            $file_type = $_FILES[\'afile\'][\'type\'];
            $file_loc = $_FILES[\'afile\'][\'tmp_name\'];
            $folder="wp-content/themes/jobify-extended/uploads/";
            $data = array(
                \'voornaam\' => $_POST[\'avoornaam\'],
                \'familienaam\' => $_POST[\'afamilienaam\'],
                \'comment\' => $_POST[\'amsg\'],
                \'afile\' => rand(1000,100000)."-".$_FILES[\'afile\'][\'name\'],
                \'size\' => $_FILES[\'afile\'][\'size\'],
                \'type\' => $_FILES[\'afile\'][\'type\'],



            );

            // new file size in KB
            $new_size = $file_size/1024;  
            // new file size in KB

            // make file name in lower case
            $new_file_name = strtolower($file);
            // make file name in lower case

            $final_file=str_replace(\' \',\'-\',$new_file_name);

            $format = array(
                \'%s\',
                \'%s\'
            );

            if(move_uploaded_file($file_loc,$folder.$final_file)) 
            {
                $success=$wpdb->insert( $table, $data, $format,$folder);
                if($success){
                ?>
                    <script>
                    alert(\'successfully uploaded\');
                    window.location.href=\'sollicitatie-formulier?success\';
                    </script>
    <?php

}
}
}


else   {
    ?>

        <form action="" method="post" enctype="multipart/form-data">
        <label for="voornaam">Voornaam: </label><input type="text" name="avoornaam" required="">
        <label for="familienaam">Familienaam: </label><input type="text" name="afamilienaam" required="">
        <label for="comment">bericht: </label><textarea type="text" name="amsg"></textarea>
        <input type="file" name="afile" accept=".pdf, .doc, .docx" />
        <input type="submit" name="btn-upload" value="upload"</input>
        </form>

    <?php
        if(isset($_GET[\'success\']))
        {
    ?>
        <label>Kandidaat succesvol toegevoegd...  <a href="/bekijk/">click hier om kandidaten te bekijken.</a></label>
        <?php
    }
    else if(isset($_GET[\'fail\']))
    {
        ?>
        <label>Oeps...een probleem! Probeer opnieuw</label>
        <?php
    }
    else
    {
        ?>
        <label>Try to upload any files(PDF, DOC, DOCX)</label>

 <?php } }

        return ob_get_clean();

        }
    }

    add_shortcode( \'sollicitatie_formulier\', \'sollicitatie_formulier\' );

结束

相关推荐

Onepage with shortcodes

我构建了一个单页wordpress主题,为了在一个页面中显示我的所有页面,我使用了一个循环,但短代码不起作用(例如:联系表单7:表单正在显示但根本不起作用)。这是我的代码: <?php /* Template Name: Pulse one page */ $this_page = $post->ID; ge