How to create a Lessons Playlist using LearnDash plugin? - wordpress

I am creating a Playlist using LearnDash Plugin,
Below is my code, This code works good but I can't get a video of a particular Lesson
Is there anyone who knows how to fix this?
add_shortcode( 'playlist', 'playlist_for_LMS' );
function playlist_for_LMS() {
global $post;
$user_id = get_current_user_id();
$courses = learndash_user_get_enrolled_courses( $user_id, array(), true );
$lessons = learndash_get_course_lessons_list( $courses[0], $user_id, array(), true );
foreach ($lessons as $lesson){
$postID = $lesson['post']->lesson_video_url;
$postImage = get_the_post_thumbnail($lesson['post']->ID);
$postTitle = $lesson['post']->post_title;
$postLink = get_post_permalink($lesson['post']->ID);
echo ''.$postTitle.' '.$postID.'<br>';
}
}

You use the below code and whats are the your output and go through your out put of fetched data. And also you can get errors if you have.
echo '<pre>';
print_r($lessons);

Related

Want to sort posts in a way current user's favorite posts are shown on top

I'm using "Search and filter" plugin, and "WP Favorites" plugin. I'm using following code in custom template
if ($query->have_posts())
{
while ($query->have_posts())
{
$query->the_post();
$meta_values = get_post_meta( get_the_ID() );
$post_meta_value = get_post_meta( get_the_ID(), '_audioigniter_tracks',true );
foreach($post_meta_value as $name) {
$kjkj = get_the_ID();
echo do_shortcode("[favorite_button post_id='$kjkj']");?>
<a class="a" href="<?php echo $name['track_url']?>"><?php the_title() ?></a><br/><?php
}
}
}
While in functions.php I'm using following code to sort but it's not working as I'm missing something related to user favorite posts meta
function myfunctions( $query, $sfid ) {
//global $wpdb, $query, $wp;
if($sfid==133203)
{
$favs = get_user_favorites();
foreach($post_meta_valuee as $fst) {
$query['order'] = 'DESC';
$query['orderby'] = 'meta_value';
$query['meta_key'] = 'simplefavorites_count';
}
return $query;
}
add_filter( 'sf_edit_query_args', 'myfunctions', 20, 2 );
Problem is in functions.php code, I'm somehow not able to let the query know info about users favorites (simplefavorites_count is total count by all users, not by current user). So I need help here.

Create a Cron job after every post publish

I have a situation where i want to create a cron job after every WordPress blog post publish. Cron job will run after 7 days of publishing post and check if all the users have viewed the posts and email the list of not viewed post by users list. How can i achieve this. Its a WordPress website and there are around 300+ users, after each blog post publish user gets notification. So want to check who has viewed and who has not viewed the post.
If you want to start the cron job only the first time a post is published, you'll want to use the draft_to_publish hook.
draft_to_publish
Well there are Plugins capable of managing Stats and tracking users activities. I'm not sure which one fit to your needs but I'll try to give a simple approach to do it by yourself.
Tracking
We can set a custom field for those (posts/post-types) where we will store a list of users who read that post. Let's call it readerslist
On the single.php file we will add these lines to Update this field.
$list = explode(',',get_post_meta($post->ID, 'readerslist', true));
$user = get_current_user_id();
if (!in_array($user,$list)){
array_push($list, $user);
update_post_meta( $post->ID, 'readerslist', implode(",", $list) );
}
Now that we can retrieve who viewed the article? We could later find those who didn't read it yet.
Then in functions.php we could set up a hook that will execute when a post get published, to set a single scheduled cron job to send the absent readers.
<?php
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
{
if ($new_status != 'publish') {
return;
}
//You better add limitations to choose specific post_types you are targeting
$postid=$post->ID;
add_cron($postid);
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
function add_cron($postid){
$nxtweek = strtotime("+1 week");
add_action('sendemail', 'stats_mail', 10, 1);
wp_schedule_single_event(time() + $nxtweek, 'sendemail',array( $postid);
}
function stats_mail($postid)
{
$readerslist = get_post_meta( $postid, 'readerslist', true );
$users = get_users( array( 'fields' => array( 'ID' ) ) );
$absents = array_diff($users, explode(",",$readerslist));
//Now that you got the list of absent users
$names='';
foreach ($absents as $x){
$names.=$x.' : '.get_user_meta( $x,'first_name' , true ).' '.get_user_meta( $x,'last_name' , true ).'<br>';
}
$title=get_the_title($postid);
$to = 'you#example.com';
$subject = 'Users who didn\'t read '.$title;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $names, $headers );
}
?>
Notice : I didn't tested the above code ! I made you this in order to show you how to do it. But there isn't much work to do here. Good luck
I tried your code, it works fine, but i am not able to make it working as i wanted, can you look into it. I don't know what i am doing wrong.
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
if ($new_status != 'publish') {
return;
}
$postid=$post->ID;
add_cron($postid);
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
function add_cron($postid){
$nxtweek = strtotime("+1 week");
add_action('sendemail', 'stats_mail', 10, 1);
if ( ! wp_next_scheduled( 'sendemail') ) {
wp_schedule_event(time() + $nxtweek, $nxtweek, 'sendemail', array($postid));
// i want to run cron every 7 days from the date of publishing post, until all user reads it, i tried this code but not working.
// wp_mail( 'email#company.com', $postid, 'test', null );
// For testing purpose if i send mail from here, it goes.
}
}
function stats_mail($postid) {
wp_mail( 'email#company.com', $postid, 'test', null );
// this doesnt work.
$readerslist = get_post_meta( $postid, 'readerslist', true );
$users = get_users( array( 'fields' => array( 'ID' ) ) );
$absents = array_diff($users, explode(",",$readerslist));
//Now that you got the list of absent users
$names='';
foreach ($absents as $x){
$names.=$x.' : '.get_user_meta( $x,'first_name' , true ).' '.get_user_meta( $x,'last_name' , true ).'<br>';
}
$title=get_the_title($postid);
$to = 'email#company.com';
$subject = 'Users who didn\'t read '.$title;
$headers = array('Content-Type: text/html; charset=UTF-8');
//wp_mail( $to, $subject, $names, $headers );
}

Show 'This page not translated' if translation doesn't exist. WP and WPML

Well, I have a WordPress site with WPML. Not all of my pages are translated to EN version. And I know to realize following. When somebody tries to go to URL which doesn't exist I want to show him/her 'Not translated page' (with a recommendation to observe this content in another language).
How can I realize it? Thanks!
add_action('wp_head', 'wpml_custom');
function wpml_custom() {
global $wp_query;
$postId = $wp_query->post->ID;
$postType = $wp_query->post->post_type;
$args = [
'element_id' => $postId, 'element_type' => $postType
];
$translation = apply_filters( 'wpml_element_language_details', null, $args );
$currentLang = apply_filters( 'wpml_current_language', null );
if (preg_match('/^\/$/', $_SERVER['REQUEST_URI'])) {
return;
} elseif ($translation->language_code !== $currentLang) {
require get_template_directory() . '/no-translation.php';
exit();
}
}

Exclude current post from "recent posts widget"

In a plugin for displaying recent posts in your sidebar widget, how can we apply a filter to the plugin's functions.php so that it won't include the current page/post in the display?
The plugin author replied, before he entered a long silence: "You can add custom parameter to the rpwe_default_query_arguments filter. Just add exclude => get_the_ID() to the filter."
Is it here, that we add it?
// Allow plugins/themes developer to filter the default query.
$query = apply_filters( 'rpwe_default_query_arguments', $query );
How?
This is the plugin: https://wordpress.org/plugins/recent-posts-widget-extended/
I found some guidance that appears to be quite simple
but then it results in errors in my site (localhost) while trying to correct the syntax. => seems to be not correctly used.
This is what I have so far:
add_filter( 'rpwe_default_query_arguments', 'rpwe_exclude_current' );
function rpwe_exclude_current ( $query ) {
'exclude' => get_the_ID()
$posts = new WP_Query( $query );
return $posts;
}
Here is the answer that worked in my situation:
add_filter( 'rpwe_default_query_arguments', 'my_function_name' );
function my_function_name( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
Here is the site where I found it.

Why is not get_post_meta working?

i am using following get_post_meta function but it is not showing any values, it is showing blank result
$meta_values = get_post_meta( $post_id, 'salary', true );
I am able to see the value present in phpmyadmin .I am using it in a plugin in action(save_post,sftoproject), but it is showing any value in my code.
Here's my code
add_action('save_post','user_sf_project');
function user_sf_project($post_id){
$SOAP_CLIENT_BASEDIR = plugin_dir_path(__FILE__).'Force.com-Toolkit-for-PHP/soapclient/';
require_once ($SOAP_CLIENT_BASEDIR.'/SforcePartnerClient.php');
require_once ($SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');
require_once ('Force.com-Toolkit-for-PHP/Samples/userAuth.php');
try {
$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection($SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');
$mylogin = $mySforceConnection->login("username", "password");
$postdata = get_post($post_id);
$meta_values = get_post_meta( $post_id, 'salary', true );
$fields = array (
'Name' => $post_id,
'First_Name__c' => $postdata->post_title,
'Salary__c' => $meta_values,
);
$sObject = new SObject();
$sObject->fields = $fields;
$sObject->type = 'NewObject__c';
$createResponse = $mySforceConnection->create(array($sObject));
print_r($createResponse);
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}
From the WP docs:
It is a wrapper for get_metadata('post'). To return all of the custom
fields, see get_post_custom().
You must use get_post_custom() function (not get_post_meta) for custom post types, but I highly recommend that you always use get_metadata instead of get_post_meta() and get_post_custom() to avoid simple problems...
Example for get_metadata():
get_metadata('your_custom_post_type', $post_id, 'salary', true)

Resources