Вопрос:

Wordpress member acces to custom page/template

php wordpress access

75 просмотра

1 ответ

10 Репутация автора

I have made some custom templates in my wordpress and used it to build 2 pages with. Then I have tried the Members plugin for access control. It work on the pages, but it still shows my custom PHP code on that page. It there a better plugin for this or should my custom php include som standard code from wordpress to be included in the access control?

<?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();
?>
Автор: KristianC Источник Размещён: 10.03.2017 05:57

Ответы (1)


0 плюса

700 Репутация автора

Решение

Something like this?

You can put this code at the start of your custom theme template to restrict access.

<?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(); ?>

Show warning instead of content on regular page

<?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

Автор: Adam K. Размещён: 10.03.2017 01:05
Вопросы из категории :
32x32