Giter VIP home page Giter VIP logo

Comments (5)

mikejolley avatar mikejolley commented on July 17, 2024

'dlm_options_end' action is pretty straightforward - you just hook in and output your checkbox html. The post ID is passed to the action should you wish to get_post_meta

And for saving its just a case of something like:

add_action( 'dlm_save_meta_boxes', 'your_save_function', 1, 2 );

function your_save_function( $post_id, $post ) {
  $checkbox = ( isset( $_POST['checkbox'] ) ) ? 'yes' : 'no';
  update_post_meta( $post_id, 'checkbox', $checkbox);
}

from download-monitor.

SmashBrando avatar SmashBrando commented on July 17, 2024

Thanks for the help. I was able to get my additional postmeta added and saved via -

//add additional meta to download monitor post screen.
function add_custom_dm_options( $post ) {
        global $post, $thepostid;
        $thepostid = $post->ID;

        echo '<p class="form-field form-field-checkbox">
            <input type="checkbox" name="_act_sub" id="_act_sub" ' . checked( get_post_meta( $thepostid, '_act_sub', true ), 'yes', false ) . ' />
            <label for="_act_sub">' . __( 'Active Subscriber', 'download_monitor' ) . '</label>
            <span class="description">' . __( 'This download is only for users who belong to the Active Subscriber Role.', 'download_monitor' ) . '</span>
        </p>';

    }
add_action( 'dlm_options_end', 'add_custom_dm_options' );

//save additional meta on download monitor post.
function save_custom_dm_options( $post_id, $post ) {
  $_act_sub = ( isset( $_POST['_act_sub'] ) ) ? 'yes' : 'no';
  update_post_meta( $post_id, '_act_sub', $_act_sub );
}
add_action( 'dlm_save_meta_boxes', 'save_custom_dm_options', 1, 2 );

Below is an example of code that I am running in order to get the new meta option I created to work with a user cap, allowing download. For some reason I can only get - Call to a member function is_act_sub() on a non-object in. I have tried using global $download; with no luck.

//new rules using the act_sub post meta check and the active_subscription user role. 
function subscription_access( $download, $can_download) {
        if ( $download->is_act_sub() && ! current_user_can('active_subscription') )
            $can_download = true;

        return $can_download;
    }
add_filter( 'dlm_can_download', 'subscription_access', 10, 2 );

Any insight? Thank you in advanced.

from download-monitor.

mikejolley avatar mikejolley commented on July 17, 2024

is_act_sub() isn't a method of the download class. You need to use get_post_meta directly.

from download-monitor.

SmashBrando avatar SmashBrando commented on July 17, 2024

Thanks. I got it. I will post my code in a bit so others can use it.

from download-monitor.

SmashBrando avatar SmashBrando commented on July 17, 2024

Alright here is the code I used to do the following.

  1. Add additional meta boxes to download post screen
  2. Save post meta
  3. Get post meta value and then use it to rule if a user has permission to download based on custom user roles and capabilities.

Thanks for your help! I will post my code to my repository here Themazign Toolbox

<?php
/*
This file includes the download monitor customizations needed to add additional meta options, save meta options and new rules based on meta and user cap.
*/

//setup globals
global $post, $download;


// start add aditional post meta

//add additional meta to download monitor post screen.
function add_custom_dm_options( $post ) {
                global $post, $thepostid;
                $thepostid = $post->ID;

                echo '<p class="form-field form-field-checkbox">
                        <input type="checkbox" name="_act_sub" id="_act_sub" ' . checked( get_post_meta( $thepostid, '_act_sub', true ), 'yes', false ) . ' />
                        <label for="_act_sub">' . __( 'Active Subscriber', 'download_monitor' ) . '</label>
                        <span class="description">' . __( 'This download is only for users who belong to the Active Subscriber Role.', 'download_monitor' ) . '</span>
                </p>';

        }
add_action( 'dlm_options_end', 'add_custom_dm_options' );

//save additional meta on download monitor post.
function save_custom_dm_options( $post_id, $post ) {
  $_act_sub = ( isset( $_POST['_act_sub'] ) ) ? 'yes' : 'no';
  update_post_meta( $post_id, '_act_sub', $_act_sub );
}
add_action( 'dlm_save_meta_boxes', 'save_custom_dm_options', 1, 2 );

// end add aditional post meta


//new rules using the act_sub post meta check and the active_subscription user role.
function check_subscription( $can_download, $download ) {
            $can_download = false;
            if (current_user_can('active_subscriber_cap')){

                $active_sub_status = get_post_meta( $download->post->ID , '_act_sub', true );

                if ( $active_sub_status = 'yes' )
                $can_download = true;
            }

            elseif (current_user_can('inactive_subscriber_cap')) {
                if ( $download->is_members_only() && is_user_logged_in() )
                $can_download = true;
            }

            elseif (current_user_can('free_cap')) {
                if ( $download->is_members_only() && is_user_logged_in() )
                $can_download = true;
            }


            return $can_download;
        }
add_filter( 'dlm_can_download', 'check_subscription', 10, 2 );

?>

from download-monitor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.