Skip to content

Instantly share code, notes, and snippets.

@dcavins
dcavins / bp_docs_filter_default_access_options.php
Created March 27, 2024 13:49
Change the default access options for new BP Docs.
<?php
add_filter( 'bp_docs_get_default_access_options', 'my_change_docs_default_access_levels' );
function my_change_docs_default_access_levels( $defaults ) {
/**
* Possible access capabilities are 'read', 'edit', 'read_comments', 'post_comments',
* 'view_history', or 'manage'.
* Possible access options are 'loggedin', 'creator', or 'anyone'.
* If BuddyPress groups are activated, then additional access options are available
* to docs that are associated with a group:
* 'group-members' and 'admins-mods'.
@dcavins
dcavins / bp_docs_groups_allow_group_admins_to_manage_access.php
Created May 22, 2023 20:39
Allow group administrators and moderators to manage group-associated BuddyPress Docs.
<?php
add_filter( 'bp_docs_map_meta_caps', 'bp_docs_groups_allow_group_admins_to_manage_access', 20, 4 );
function bp_docs_groups_allow_group_admins_to_manage_access( $caps, $cap, $user_id, $args ) {
// We only want to act on the "manage" capability, in group situations.
if ( 'bp_docs_manage' !== $cap ) {
return $caps;
}
$doc = bp_docs_get_doc_for_caps( $args );
if ( empty( $doc ) ) {
return $caps;
@dcavins
dcavins / enable-global-folders-for-bpdocs.php
Last active November 9, 2022 19:24
Enable BuddyPress Docs folders for all contexts (not just groups which is the default)
<?php
// Enables folders for group, user, and global doc directories.
add_filter( 'bp_docs_enable_folders_for_current_context', '__return_true' );
// Shows the folder metabox on the doc edit screen.
add_filter( 'bp_docs_folders_force_metabox', '__return_true' );
@dcavins
dcavins / elevate-bp-docs-access-setttings.php
Last active February 21, 2022 18:58
Find all BP Docs that have perrmissive read settings ("anyone") and change them to "loggedin" or "group-members".
<?php
// Ignore folder association temporarily.
remove_filter( 'bp_docs_tax_query', 'bp_docs_folder_tax_query', 10, 2 );
$doc_args = array(
// 'folder' => ,
'posts_per_page' => 50,
'paged' => 1, // increment this to work through the docs.
);
if ( bp_docs_has_docs( $doc_args ) ) :
@dcavins
dcavins / auto-approve-some-bp-membership-requests.php
Created December 13, 2021 17:26
Automatically approve membership requests that satisfy some criteria.
<?php
/**
* If a user submits a site membership request, but has a
* 'my-school.edu' email address, bypass the manual approval of the request.
*
* @param bool $send Whether or not this membership request should be approved
* immediately and the activation email sent.
* Default is `false` meaning that the request should be
* manually approved by a site admin.
* @param array $details The details of the request.
@dcavins
dcavins / bp-docs-group-admins-can-read-and-edit.php
Last active June 17, 2021 15:52
Change BP Docs capabilities so that group admins and mods can read and edit any doc associated with the group.
<?php
/**
* Allow group admins and mods to read and edit any BP Doc
* associated with a group they moderate.
* Note: this will allow direct access at `/docs/doc-title/` but
* the docs will not be listed in the group docs directory if the
* access level "should" prevent the user from accessing it.
* So it's of limited usefulness.
*
* @param string[] $caps Primitive capabilities required of the user.
@dcavins
dcavins / disallow-creator-access-for-group-docs.php
Created June 16, 2021 14:24
BP Docs: Don't allow group docs to have "creator only" access
<?php
add_filter( 'bp_docs_get_access_options', 'remove_creator_option_for_group_docs', 10, 4 );
function remove_creator_option_for_group_docs( $options, $settings_field, $doc_id = 0, $group_id = 0 ) {
if ( ! $group_id ) {
$group_id = bp_docs_get_associated_group_id( $doc_id );
}
// If this is the Doc creation page, check to see whether a
// group id has been passed somewhere
if ( empty( $group_id ) ) {
@dcavins
dcavins / bp_docs_mark_all_new_docs_as_pending.php
Created April 13, 2021 13:39
Set every new BP doc to pending status.
<?php
/**
* Mark all new docs as pending.
*
* @param array $r The parameters to be used in wp_insert_post().
* @param object $this The BP_Docs_Query object.
* @param array $args The passed and filtered parameters for the doc
* about to be saved.
*/
function bpdocs_support_mark_all_as_pending( $save_args, $bdq_object, $passed_args ) {
@dcavins
dcavins / gtm-add-to-theme.php
Created October 5, 2020 19:24
Add the Google Tag Manager snippets to a WordPress theme's `functions.php` file.
<?php
/**
* Add the Google tag manager script in the header.
*
* @since 1.0.0
*
*/
function prefix_add_google_tag_manager_script() {
if ( function_exists( 'wp_get_environment_type' )
&& 'production' !== wp_get_environment_type() ) {
@dcavins
dcavins / media-right.html
Created November 25, 2019 21:43
Position an image to the right of related text.
<div class="Media Media--reverse">
<img src="/wp-content/uploads/sites/6/2019/11/doubtful-kitty.jpg" alt="A cat that doubts your ideas." width="100" height="100" class="Media-figure size-full wp-image-1350" />
<div class="Media-body">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer sit amet blandit lacus. Proin bibendum eros ut turpis tempor finibus. Vivamus ac scelerisque lectus. Nullam in justo vel mauris fermentum tempus.</p>
</div>
</div>