snipt

Ctrl+h for KB shortcuts

PHP

WordPress template tag to display the name/title of PARENT page.

1
2
3
4
<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?>
https://snipt.net/embed/c8c2347c8a3ad9b9ddfb265764347707/
/raw/c8c2347c8a3ad9b9ddfb265764347707/
c8c2347c8a3ad9b9ddfb265764347707
php
PHP
5
2019-06-15T01:29:25
True
False
False
May 05, 2016 at 12:13 PM
/api/public/snipt/146151/
wordpress-template-tag-to-display-the-nametitle-of-parent-page
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$parent_title</span> <span class="o">=</span> <span class="nx">get_the_title</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">post_parent</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="k">echo</span> <span class="nv">$parent_title</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
wordpress

PHP

WordPress functions.php code: modify role name

1
2
3
4
5
6
7
8
function wps_change_role_name() {
    global $wp_roles;
    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();
    $wp_roles->roles['contributor']['name'] = 'Owner';
    $wp_roles->role_names['contributor'] = 'Owner';
}
add_action('init', 'wps_change_role_name');
https://snipt.net/embed/fdf0c9f28ea3b751eb31cf0d1dd0d311/
/raw/fdf0c9f28ea3b751eb31cf0d1dd0d311/
fdf0c9f28ea3b751eb31cf0d1dd0d311
php
PHP
8
2019-06-13T22:00:56
True
False
False
Jan 17, 2016 at 05:19 PM
/api/public/snipt/143622/
wordpress-functionsphp-code-modify-role-name
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">function wps_change_role_name() {</span> </span><span id="L-2"><a name="L-2"></a><span class="x"> global $wp_roles;</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> if ( ! isset( $wp_roles ) )</span> </span><span id="L-4"><a name="L-4"></a><span class="x"> $wp_roles = new WP_Roles();</span> </span><span id="L-5"><a name="L-5"></a><span class="x"> $wp_roles-&gt;roles[&#39;contributor&#39;][&#39;name&#39;] = &#39;Owner&#39;;</span> </span><span id="L-6"><a name="L-6"></a><span class="x"> $wp_roles-&gt;role_names[&#39;contributor&#39;] = &#39;Owner&#39;;</span> </span><span id="L-7"><a name="L-7"></a><span class="x">}</span> </span><span id="L-8"><a name="L-8"></a><span class="x">add_action(&#39;init&#39;, &#39;wps_change_role_name&#39;);</span> </span></pre></div> </td></tr></table>
functions.php, wordpress

PHP

WordPress: PHP 'template tag' code to display shortcode inside a page template

<?php echo do_shortcode("[shortcode]"); ?>
https://snipt.net/embed/84539a0eede12f247b52e6b1fb2f23b1/
/raw/84539a0eede12f247b52e6b1fb2f23b1/
84539a0eede12f247b52e6b1fb2f23b1
php
PHP
1
2019-06-13T23:37:01
True
False
False
Dec 17, 2015 at 08:20 PM
/api/public/snipt/143152/
wordpress-php-template-tag-code-to-display-shortcode-inside-a-page-template
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">do_shortcode</span><span class="p">(</span><span class="s2">&quot;[shortcode]&quot;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
shortcode, tag, template, wordpress

PHP

WordPress: functions.php code to hide a single category of post from the home page

1
2
3
4
5
6
7
// No "Quick Tips" on the homepage
function preventHomepageTips($query) {
  if($query->is_home() && $query->is_main_query()) {
    $query->set('cat', '-40'); // 40 is Quick Tips's category ID
  }
}
add_action('pre_get_posts', 'preventHomepageTips');
https://snipt.net/embed/f16c5baa8f81233e018836aa2f6a699d/
/raw/f16c5baa8f81233e018836aa2f6a699d/
f16c5baa8f81233e018836aa2f6a699d
php
PHP
7
2019-06-14T11:39:36
True
False
False
Dec 02, 2015 at 08:00 AM
/api/public/snipt/142799/
wordpress-functionsphp-code-to-hide-a-single-category-of-post-from-the-home-page
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">// No &quot;Quick Tips&quot; on the homepage</span> </span><span id="L-2"><a name="L-2"></a><span class="x">function preventHomepageTips($query) {</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> if($query-&gt;is_home() &amp;&amp; $query-&gt;is_main_query()) {</span> </span><span id="L-4"><a name="L-4"></a><span class="x"> $query-&gt;set(&#39;cat&#39;, &#39;-40&#39;); // 40 is Quick Tips&#39;s category ID</span> </span><span id="L-5"><a name="L-5"></a><span class="x"> }</span> </span><span id="L-6"><a name="L-6"></a><span class="x">}</span> </span><span id="L-7"><a name="L-7"></a><span class="x">add_action(&#39;pre_get_posts&#39;, &#39;preventHomepageTips&#39;);</span> </span></pre></div> </td></tr></table>
code, functions, wordpress

PHP

WordPress: menu code to display a logged-in or logged-out menu

1
2
3
4
5
6
<?php
if ( is_user_logged_in() ) {
     wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) );
} else {
     wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
}
https://snipt.net/embed/44bd86fba7c503699d31fc5c76181aff/
/raw/44bd86fba7c503699d31fc5c76181aff/
44bd86fba7c503699d31fc5c76181aff
php
PHP
6
2019-06-14T00:20:51
True
False
False
/api/public/snipt/142383/
wordpress-menu-code-to-display-a-logged-in-or-logged-out-menu
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="k">if</span> <span class="p">(</span> <span class="nx">is_user_logged_in</span><span class="p">()</span> <span class="p">)</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">wp_nav_menu</span><span class="p">(</span> <span class="k">array</span><span class="p">(</span> <span class="s1">&#39;theme_location&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;logged-in-menu&#39;</span> <span class="p">)</span> <span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">wp_nav_menu</span><span class="p">(</span> <span class="k">array</span><span class="p">(</span> <span class="s1">&#39;theme_location&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;logged-out-menu&#39;</span> <span class="p">)</span> <span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
menu, template-tag, wordpress