Title: Wrapping do_shortcode around php
Last modified: August 24, 2016

---

# Wrapping do_shortcode around php

 *  [CMJ](https://wordpress.org/support/users/cmj/)
 * (@cmj)
 * [11 years ago](https://wordpress.org/support/topic/wrapping-do_shortcode-around-php/)
 * Hi,
 * I’m trying to use a plugin that makes the content of a page restricted or private
   unless the viewer is logged in. Shortcode is provided with the plugin, however
   the page I need to protect is a template page and doesn’t have a normal editable
   content area. It pulls property listings from the database.
 * I found an older thread here where someone helped someone achieve something quite
   similar. I think what I want to do is possible, but my php is just a bit more
   complicated and I think it’s just a matter of me using the wrong syntax. The 
   older thread I found is here [https://wordpress.org/support/topic/wrapping-do_shortcode-around-additional-php-code?replies=5](https://wordpress.org/support/topic/wrapping-do_shortcode-around-additional-php-code?replies=5)
 * The php code I need to wrap in shortcode is:
    `<?php if(isset($_GET['view'])){
   $view_type = $_GET['view']; }else{ /* Theme Options Listing Layout */ $view_type
   = get_option('theme_listing_layout'); }
 *  if( $view_type == 'grid' ){
    get_template_part("template-parts/grid-listing-
   container"); }else{ get_template_part("template-parts/listing-container"); } ?
   >
 * The shortcode I need to wrap it in is: [upme_private]content goes here[/upme_private]
 * Thanks for any ideas!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [11 years ago](https://wordpress.org/support/topic/wrapping-do_shortcode-around-php/#post-6140844)
 * Does the content come from the get_template_part functions?
 * Try it with this:
 *     ```
       <?php
       if ( isset( $_GET['view'] ) && $_GET['view'] ) {
       	$view_type = sanitize_title( $_GET['view'] );
       }else {
       	/* Theme Options Listing Layout */
       	$view_type = get_option( 'theme_listing_layout' );
       }
   
       ob_start();
   
       if ( $view_type === 'grid' ) {
       	get_template_part( "template-parts/grid-listing-container" );
       }else {
       	get_template_part( "template-parts/listing-container" );
       }
   
       $content = ob_get_contents();
       ob_end_clean();
   
       echo do_shortcode( '[upme_private]' . $content . '[/upme_private]' );
       ?>
       ```
   
 *  Thread Starter [CMJ](https://wordpress.org/support/users/cmj/)
 * (@cmj)
 * [11 years ago](https://wordpress.org/support/topic/wrapping-do_shortcode-around-php/#post-6140974)
 * Thanks for the reply. And it does indeed work! Thanks so much.
 * Before I got your reply, I actually fell upon a solution as well. I don’t know
   why it works, and it was just trial and error, so yours is probably better. But
   at any rate, I had this:
 * <?php
    ob_start(); if(isset($_GET[‘view’])){ $view_type = $_GET[‘view’]; }else{/*
   Theme Options Listing Layout */ $view_type = get_option(‘theme_listing_layout’);}
 *  if( $view_type == ‘grid’ ){
    get_template_part(“template-parts/grid-listing-
   container”); }else{ get_template_part(“template-parts/listing-container”); } 
   $comments_output = ob_get_contents(); ob_end_clean(); echo do_shortcode(‘[upme_private]’.
   $comments_output.'[/upme_private]’); ?>

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Wrapping do_shortcode around php’ is closed to new replies.

## Tags

 * [do_shortcode](https://wordpress.org/support/topic-tag/do_shortcode/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [CMJ](https://wordpress.org/support/users/cmj/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/wrapping-do_shortcode-around-php/#post-6140974)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
