Wordpress成员访问自定义页面/模板

时间:2017-03-10 05:57:27

标签: php wordpress access

我在wordpress中制作了一些自定义模板,用它来构建2页。然后我尝试了用于访问控制的Members插件。它适用于页面,但它仍然在该页面上显示我的自定义PHP代码。它有一个更好的插件,或者我的自定义php是否包含来自wordpress的som标准代码才能包含在访问控制中?

<?php /* Template Name: Storfanger_indberet */ ?>
<?php
if ( ! defined( 'ABSPATH' ) ) exit;

get_header(); ?>

<div id="primary" <?php generate_content_class();?>>
    <main id="main" <?php generate_main_class(); ?>>
        <?php do_action('generate_before_main_content'); ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <?php get_template_part( 'content', 'page' ); ?>

            <!--My code begin -->

            <?php
If($_POST['Submit'])
{
    // run validation if you're not doing it in js
    global $wpdb;

    $medlemsnr=$_POST['medlemsnr'];
    $navn=$_POST['navn'];
    $baadnavn=$_POST['baadnavn'];
    $art=$_POST['art'];
    $vaegt=$_POST['vaegt'];
    $billede=$_POST['billede'];

    if($wpdb->insert(
                        'ct_storfanger_indberetninger',
                        array(
                        'Medlemsnr' => $medlemsnr,
                        'navn' => $navn,
                        'bådnavn' =>$baadnavn,
                        'art' =>$art,
                        'vægt' =>$vaegt,
                        'billede' =>$billede
                        )
                    ) == false) wp_die('Der var en fejl i indsendelsen. Kontakt venligst webmaster'); else echo 'Tak for din indsendelse. Den vil fremgå af listen så snart den er godkendt af juryen!<p />';
?>
<?php
}
else // else we didn't submit the form, so display the form
{
?>

<form action="" method="post" id="form">
<table style="border:none;">
                <tr>
                <td style="border:none;width:25%">Medlemsnr</td>
                <td style="border:none;"><input name="medlemsnr"     type="text" value="" /></td>
                </tr>
            <tr>
            <td style="border:none;">Navn</td>
            <td style="border:none;"><input name="navn" type="text" value="" /></td>
            </tr>
            <tr>
            <td style="border:none;">Bådnavn</td>
            <td style="border:none;"><input name="baadnavn" type="text" value="" /></td>
            </tr>
            <tr>
            <td style="border:none;">Art</td>
            <td style="border:none;">
                <?php 
                global $wpdb;
                $retrieve_data = $wpdb->get_results( "SELECT Art FROM ct_storfanger_arter");
            ?>
            <SELECT NAME="art"><option value=""></option>
            <?php
                foreach ($retrieve_data as $retrieved_data){ 
                    echo "<option value='" . $retrieved_data->Art . "'>" . $retrieved_data->Art . "</option>";
                }
            ?>
            </SELECT></td>
            </tr>
            <tr>
            <td style="border:none;">Vægt i gram</td>
            <td style="border:none;"><input name="vaegt" type="text" value="" /> gram</td>
            </tr>
            <tr>
            <td style="border:none;">Billede</td>
            <td style="border:none;"><input name="billede" type="file" value="" /></td>
            </tr>
            </table>
            <table style="border:none;">
            <tr>
            <td style="border:none;"><input type="submit" name="Submit" id="formsubmit" value="Indsend" /></form></td>
            </tr>
            </table>

            </form>
<?php
    } // end else no post['submit']
?>              

            <!--My code ends -->

            <?php
            // If comments are open or we have at least one comment, load up the comment template
            if ( comments_open() || '0' != get_comments_number() ) : ?>
                <div class="comments-area">
                    <?php comments_template(); ?>
                </div>
            <?php endif; ?>

        <?php endwhile; // end of the loop. ?>
        <?php do_action('generate_after_main_content'); ?>
    </main><!-- #main -->
</div><!-- #primary -->
<?php 
do_action('generate_sidebars');
get_footer();
?>

1 个答案:

答案 0 :(得分:0)

这样的东西?

您可以将此代码放在自定义主题模板的开头,以限制访问。

<?php /* Template Name: Admin/Editor access */

      if(!is_user_logged_in())$getOut=true;//Check if visitor is even logged in
      elseif(current_user_can('editor') || current_user_can('administrator')){//Is user administrator or editor?
         $getOut=false;//User can stay and view the content (even process scripts)
      }
      if($getOut){//Should the user be kicked out?
      echo"<html><body>";
         echo "<script type='text/javascript'>alert('You don\'t have permission to do that');"
              ."window.location.href = '/';</script></body></html>";//Throw out warning
         exit();//Anything below won't be processed
      }
   get_header(); ?>

在常规页面上显示警告而不是内容

<?php /* Template Name: Storfanger_indberet */ ?>
<?php
if (!defined('ABSPATH')) exit;

get_header(); ?>

    <div id="primary" <?php generate_content_class(); ?>>
        <main id="main" <?php generate_main_class(); ?>>
            <?php do_action('generate_before_main_content'); ?>

            if(!is_user_logged_in())//Check if visitor is even logged in
                $getOut=true;
            elseif(current_user_can('editor') || current_user_can('administrator')){//Is user administrator or editor?
                $getOut=false;//User can stay and view the content (even process scripts)
            }
            if($getOut){//Should the user be kicked out?
                echo"Access denied";
            }else while ( have_posts() ) : the_post(); ?>

            <?php get_template_part('content', 'page'); ?>

            <!--My code begin -->

            <?php
            If ($_POST['Submit']) {
                // run validation if you're not doing it in js
                global $wpdb;

                $medlemsnr = $_POST['medlemsnr'];
                $navn = $_POST['navn'];
                $baadnavn = $_POST['baadnavn'];
                $art = $_POST['art'];
                $vaegt = $_POST['vaegt'];
                $billede = $_POST['billede'];

                if ($wpdb->insert(
                        'ct_storfanger_indberetninger',
                        array(
                            'Medlemsnr' => $medlemsnr,
                            'navn' => $navn,
                            'bådnavn' => $baadnavn,
                            'art' => $art,
                            'vægt' => $vaegt,
                            'billede' => $billede
                        )
                    ) == false
                ) wp_die('Der var en fejl i indsendelsen. Kontakt venligst webmaster'); else echo 'Tak for din indsendelse. Den vil fremgå af listen så snart den er godkendt af juryen!<p />';
                ?>
                <?php
            } else // else we didn't submit the form, so display the form
            {
                ?>

                <form action="" method="post" id="form">
                    <table style="border:none;">
                        <tr>
                            <td style="border:none;width:25%">Medlemsnr</td>
                            <td style="border:none;"><input name="medlemsnr" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td style="border:none;">Navn</td>
                            <td style="border:none;"><input name="navn" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td style="border:none;">Bådnavn</td>
                            <td style="border:none;"><input name="baadnavn" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td style="border:none;">Art</td>
                            <td style="border:none;">
                                <?php
                                global $wpdb;
                                $retrieve_data = $wpdb->get_results("SELECT Art FROM ct_storfanger_arter");
                                ?>
                                <SELECT NAME="art">
                                    <option value=""></option>
                                    <?php
                                    foreach ($retrieve_data as $retrieved_data) {
                                        echo "<option value='" . $retrieved_data->Art . "'>" . $retrieved_data->Art . "</option>";
                                    }
                                    ?>
                                </SELECT></td>
                        </tr>
                        <tr>
                            <td style="border:none;">Vægt i gram</td>
                            <td style="border:none;"><input name="vaegt" type="text" value=""/> gram</td>
                        </tr>
                        <tr>
                            <td style="border:none;">Billede</td>
                            <td style="border:none;"><input name="billede" type="file" value=""/></td>
                        </tr>
                    </table>
                    <table style="border:none;">
                        <tr>
                            <td style="border:none;"><input type="submit" name="Submit" id="formsubmit"
                                                            value="Indsend"/>
                </form></td>
                </tr>
                </table>

                </form>
                <?php
            } // end else no post['submit']
            ?>

            <!--My code ends -->

            <?php
            // If comments are open or we have at least one comment, load up the comment template
            if (comments_open() || '0' != get_comments_number()) : ?>
                <div class="comments-area">
                    <?php comments_template(); ?>
                </div>
            <?php endif; ?>

            <?php endwhile; // end of the loop. ?>
            <?php do_action('generate_after_main_content'); ?>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php
do_action('generate_sidebars');
get_footer();
?>

Working demontration