Code & Me
WordPress Code Snippet Repository
  • Home
  • Featured
  • Recently Added
  • About
  • Contact

How To Set Role And Capabilities In WordPress

September 30, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The following snippet will let you set role and capabilities in WordPress.

Add this capabilities for every custom post type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[edit_post]          => "edit_{$capability_type}"
[read_post]          => "read_{$capability_type}"
[delete_post]          => "delete_{$capability_type}"
[edit_posts]          => "edit_{$capability_type}s"
[edit_others_posts]     => "edit_others_{$capability_type}s"
[publish_posts]          => "publish_{$capability_type}s"
[read_private_posts]     => "read_private_{$capability_type}s"
[delete_posts]           => "delete_{$capability_type}s"
[delete_private_posts]   => "delete_private_{$capability_type}s"
[delete_published_posts] => "delete_published_{$capability_type}s"
[delete_others_posts]    => "delete_others_{$capability_type}s"
[edit_private_posts]     => "edit_private_{$capability_type}s"
[edit_published_posts]   => "edit_published_{$capability_type}s"
 
edit_members
read_members
delete_members
edit_memberss
edit_others_memberss
publish_memberss
read_private_memberss
delete_memberss
delete_private_memberss
delete_published_memberss
delete_others_memberss
edit_private_memberss
edit_published_memberssAdd this capabilities for every custom post type.
[edit_post]          => "edit_{$capability_type}"
[read_post]          => "read_{$capability_type}"
[delete_post]          => "delete_{$capability_type}"
[edit_posts]          => "edit_{$capability_type}s"
[edit_others_posts]     => "edit_others_{$capability_type}s"
[publish_posts]          => "publish_{$capability_type}s"
[read_private_posts]     => "read_private_{$capability_type}s"
[delete_posts]           => "delete_{$capability_type}s"
[delete_private_posts]   => "delete_private_{$capability_type}s"
[delete_published_posts] => "delete_published_{$capability_type}s"
[delete_others_posts]    => "delete_others_{$capability_type}s"
[edit_private_posts]     => "edit_private_{$capability_type}s"
[edit_published_posts]   => "edit_published_{$capability_type}s"
edit_members
read_members
delete_members
edit_memberss
edit_others_memberss
publish_memberss
read_private_memberss
delete_memberss
delete_private_memberss
delete_published_memberss
delete_others_memberss
edit_private_memberss
edit_published_memberss

Snippet Credit/Source: WordPress Codex

How To Set WordPress Options As Value

September 21, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The following snippet will let you set WordPress options as value.

1
2
3
4
5
6
<?php
  global $options;
  foreach ($options as $value) {
   if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
  }
?>

List Author Comments On Author Page

September 12, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to list all the author comments on authors page.

1
2
3
4
5
6
7
8
9
10
11
12
13
function wpse19316_author_comments( $length )
{
    $final_length = (int) $length;
    $author_comments = get_comments( array( 'ID' => $GLOBALS['authordata']->ID ) );
    foreach ( $author_comments as $comment )
    {
        $comment_length = sublen( $comment->comment_content );
        $comment_excerpt = $comment->comment_content;
        if ( $comment_length > $final_length )
            $comment_excerpt = substr( $comment->comment_content, $final_length );
        echo $comment_excerpt.'<br />';
    }
}

Snippet Source/Credit: StachExchange

WordPress Display Page

September 5, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will put page directly into your page template.

1
2
3
4
5
<?php
 $id = 1; // page ID to display
 $p = get_page($id);
 echo apply_filters('the_content', $p->post_content);
 ?>

Snippet Source/Credit: Snipplr

Removing WordPress Version From Head And Feeds

August 31, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to remove WordPress version from head and feeds.

1
2
3
4
function complete_version_removal() {
    return '';
}
add_filter('the_generator', 'complete_version_removal');

Snippet Source/Credit: Snipplr

Delete WordPress Post Revisions

August 20, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The snippet below will allow you to delete WordPress post revisions.

1
2
3
4
$wpdb->query( "
DELETE FROM $wpdb->posts
WHERE post_type = 'revision'
" );

Snippet Source/Credit: Hardeep Asrani

Showing Content For Logged In Users

August 11, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The snippet below will let you show content for logged-in users.

1
2
3
4
5
6
7
8
9
10
<?php
add_shortcode("hide","hide_shortcode");
function hide_shortcode($x,$text=null){
if(!is_user_logged_in()){
return "You have to been registered and logged in to see this content";
}else{
return do_shortcode($text);
}
}
?>

Snippet Source/Credit: Snipplr

Disable WordPress Plugins Update

August 3, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The snippet below will let you disable WordPress plugins update.

1
2
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );

Snippet Source/Credit: WP Snippets

Delete Link In Comments In Your WordPress Website

July 30, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The snippet will let you delete link in comments in your WordPress website.

1
2
3
4
5
6
function delete_comment_link($id) {
    if (current_user_can('edit_post')) {
        echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&c='.$id.'">borrar</a> ';
        echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&dt=spam&c='.$id.'">spam</a>';
    }
}

Snippet Source/Credit: Snipplr

Force Categories Widget To Show Empty Categories In WordPress

July 20, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The snippet below will let you have force categories widget to show empty categories in WordPress.

1
2
3
4
5
6
7
8
9
<?php
add_filter( 'widget_categories_args', 'mytheme_widget_cat_args' );
function mytheme_widget_cat_args($cat_args) {
    // the default for "hide_empty" = 1, so
    $cat_args['hide_empty'] = 0;
    // we can override any other defaults here too
    return $cat_args;
}
?>

Snippet Source/Credit: WordPress Codex

1 2 3 4 5 >»

Recently Added Snippets

  • How To Set Role And Capabilities In WordPress
  • How To Set WordPress Options As Value
  • List Author Comments On Author Page
  • WordPress Display Page
  • Removing WordPress Version From Head And Feeds

Search The Snippet

Featured Snippets

  • How To Set WordPress Options As Value
  • WordPress Display Page
  • Removing WordPress Version From Head And Feeds
  • Delete Link In Comments In Your WordPress Website
  • Creating Database For Your WordPress Website
Code & Me
  • About
  • Recently Added
  • Advertise
  • Terms
  • Privacy Policy
  • Contact
© 2014-19 CodeAndMe.
Proud Member Of G2One Network Family.

↑ Back to top