Change CoursePress module pagination to next/previous + add subtitle to unit list

1) I need to change the pagination between course modules to next/previous links instead of the default page numbers

2) Also need to either add a subtitle to the unit list or display the first page title, whichever is easiest. I had managed to do the latter in the free version of CoursePress by using the [course_unit_page_title] shortcode , but upon upgrading to Pro, this is no longer working. I've attached a screenshot demonstrating what I mean.

Would vastly prefer to do both these things without having to modify any core files. Thanks :slight_smile:

  • Adam Czajczyk

    Hello janineisabelle,

    I hope you're well today and thank you for your question.

    The "[course_unit_page_title]" shortcode remains undocumented but it's still is left untouched in CoursePress Pro source code, therefore it should be working. As you mentioned - it's stopped working so could you please provide me with some more details on this:

    - do you see the shortcode now instead of its output or nothing shows up?
    - does it show any errors instead?
    - have you made any other changes to your site besides switching to CoursePress Pro (such as e.g. changing theme or adding some custom changes to it) recently?
    - where on your site and in what form (e.g. what attributes) the shortcode is placed?

    Please advise! It would also be great if you could grant me a support access to your site so I could take a closer look. To do this please follow this guide:

    https://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    As for unit pagination. The "numbers based' pagination is built-into the plugin ("hard-coded") and currently there's no way to change it. This would require changes in plugin's code so you may want to post a separate thread in our "Features and Feedback" forum, suggesting that idea. If more members of our community will support it, our developers may include it in future features list.

    Kind regards,
    Adam

  • janineisabelle

    Hi Adam,

    Nothing shows up now. There are no errors and I haven't made any changes to the unit archive page template since upgrading to Pro. I've granted support access so you can take a look.

    I'm aware that changing the pagination would involve overriding the current plugin code. I'm asking what the best method is for doing that. If it involves modifying the core files, so be it, although obviously a hook or snippet would be preferable. Since what I'm asking for is WordPress' standard pagination method, one would assume there must be a way to implement that. Suggesting it as a feature that may or may not be added in the future doesn't really help me at the moment.

  • Milan

    Hello @janineipwilliams,

    Hope you are well today and thanks for asking us. :slight_smile:

    If editing core files are okay to you then please follow these steps and you will have next/previous links for your module pagination.
    #1) open up \wp-content\plugins\coursepress\includes\functions.php file and find function named "coursepress_unit_module_pagination" there.
    Replace that functions with this one,

    /**
     * Unit unit module pagination
     */
    function coursepress_unit_module_pagination( $unit_id, $pages_num, $check_is_last_page = false ) {
    	global $wp, $wp_query, $paged, $coursepress_modules, $coursepress;
    
    	if ( !isset( $unit_id ) ) {// || !is_singular()
    		//<br clear="all">
    		echo '<div class="navigation module-pagination" id="navigation-pagination"></div>';
    
    		return;
    	}
    
    	$paged = isset( $wp->query_vars[ 'paged' ] ) ? absint( $wp->query_vars[ 'paged' ] ) : 1;
    
    	$max = intval( $pages_num ); //number of page-break modules + 1
    
    	$wp_query->max_num_pages = $max;
    
    	if ( $check_is_last_page ) {
    		if ( $max <= 1 || ( $max == $paged ) ) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    //<br clear="all">
    	if ( $wp_query->max_num_pages <= 1 ) {
    		echo '<div class="navigation module-pagination" id="navigation-pagination"></div>';
    
    		return;
    	}
    //<br clear="all">
    	echo '<div class="navigation module-pagination" id="navigation-pagination"><ul>' . "\n";
    
    	for ( $link_num = 1; $link_num <= $max; $link_num ++ ) {
    		$enabled = "";
    		if ( $coursepress->is_preview( $unit_id, $link_num ) ) {
    			$enabled = 'enabled-link';
    		} else {
    			if ( isset( $_GET[ 'try' ] ) ) {
    				$enabled = 'disabled-link';
    			}
    		}
    
    		$class = ( $paged == $link_num ? ' class="active ' . $enabled . '"' : ' class="' . $enabled . '"' );
    		if( $link_num == ( $paged - 1 ) ){
    			printf( '<li%1$s data-test="prev"><a href="%2$s">%3$s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link_num ) ), "Previou" );
    		}else if( $link_num == ( $paged + 1 ) ){
    			printf( '<li%1$s data-test="next"><a href="%2$s">%3$s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link_num ) ), "Next" );
    		}else{
    			continue;
    			printf( '<li%1$s><a href="%2$s">%3$s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link_num ) ), $link_num );
    		}
    	}
    
    	echo '</ul></div>' . "\n";
    }

    And for displaying unit subpage title, You need to provide unit id in shortcode this way,

    <?php echo do_shortcode( '[course_unit_page_title unit_id="' . $unit_id . '"]' ); ?>

    $unit_id is the id of unit being processed.

    Hope this helps you.

    Cheers,
    Milan

  • Adam Czajczyk

    Hello janineisabelle!

    I checked the code on my test site but I didn't found a reason for that behavior. Since it's Milan's code he'd be the most qualified to debug and adjust it I think. He's not online at the moment so I notified him of your response and he'll be back with you shortly (as soon as he gets back online for work).

    Please keep an eye on this thread.
    Best regards,
    Adam

  • Milan

    Hello @janineipwilliams,

    Hope you are well today and sorry for being late here.

    Would you be able to send in your FTP and wp-admin credentials so I could take a closer look ?

    You can send the info through our private contact form: https://premium.wpmudev.org/contact/

    Select "I have a different question" for your topic - this and the subject line ensure that it gets assigned to me :slight_smile:

    Send in:

    Subject: "Attn: Milan Savaliya"
    -WordPress admin username
    -WordPress admin password
    -login url
    -FTP credentials (host/username/password)
    -link back to this thread for reference
    -any other relevant urls

    Once we have this information then I can debug the issue further :slight_smile:

    Look forward to hearing back!

    Cheers,
    Milan

  • Milan

    Hello @janineipwilliams,

    I hope you are having a good day and sorry for being late on this but you know I was working on code snippet for you. :slight_smile:

    Now finally code snippet is ready and I've succcesfully injected into your CoursePress Pro's core files. And it it working like charm! Here in this post I am describing what changes I've made in which files so you can make those on your own when you decide to upate plugin. Consider this description for future reference. :slight_smile:

    Workign Video confirmation that Unit's module's previous and next navigation is working fine is here,
    http://screencast.com/t/iwcX0fNxZTVc

    Now let me describe changes that I undertook in order to make this work.

    #1) Commented out these lines of code of function coursepress_unit_module_pagination in file wp-content\plugins\coursepress\includes\functions.php.

    $class = ( $paged == $link_num ? ' class="active ' . $enabled . '"' : ' class="' . $enabled . '"' );
    printf( '<li%1$s><a href="%2$s">%3$s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link_num ) ), $link_num );

    And added this one just below it,

    //=====================================================================
    		//	/ Changed code to display next and previous navigation menu items
    		//=====================================================================
    
    		$class = ( $paged == $link_num ? ' class="active ' . $enabled . '"' : ' class="' . $enabled . '"' );
    		if( $link_num == ( $paged - 1 ) ){
    			printf( '<li%1$s><a data-htmltextisnotint="yes" data-linknum="%2$s" href="%3$s">%4$s</a></li>' . "\n", $class, $link_num, esc_url( get_pagenum_link( $link_num ) ), "Previous" );
    		}else if( $link_num == ( $paged + 1 ) ){
    			printf( '<li%1$s><a data-htmltextisnotint="yes" data-linknum="%2$s" href="%3$s">%4$s</a></li>' . "\n", $class, $link_num, esc_url( get_pagenum_link( $link_num ) ), "Next" );
    		}else{
    			printf( '<li%1$s style="display: none"><a href="%2$s">%3$s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link_num ) ), $link_num );
    		}
    
    		//=====================================================================
    		//	/ Changed code to display next and previous navigation menu items
    		//=====================================================================

    Screenshot of changes.

    #2) Updated javascript function change_unit_page of file \wp-content\plugins\coursepress\js\coursepress-front-elements.js.
    Added this bit of code in it

    //WPMU DEV --> Support Staff Modification.
        //First we need to determine is clicked target link is the one which is modified in "coursepress_unit_module_pagination" function ?
        if( targetObj.data('htmltextisnotint') == 'yes' ){
            // If that is the link then,
            //1. Grab form's action attribute in which link resides
            //2. Update form's action attribute with the link's href value
            var action = jQuery("#modules_form").attr("action" );
            jQuery("#modules_form").attr("action", action + 'page/' + parseInt( targetObj.data('linknum') ) + '/');
    
            console.log( jQuery("#modules_form").attr("action") );
    
            var active_page = jQuery('#navigation-pagination .active a').html();
    
            console.log( 'Active page no: ' + active_page );
    
            //This button is the clicked link's number
            var button = targetObj.data( 'linknum' );
    
            if( button < active_page )
                $('#modules_form').append('<input type="hidden" id="dont_save_student_progress_indication" name="dont_save_student_progress_indication" />');
    
            $("#modules_form" ).submit();
            return;
        }

    after these lines of function.

    var $ = jQuery;
        var target = e.currentTarget;
    
        var targetObj = jQuery( target );

    Screenshot of changes.

    Note: Dear I want to note here that change to core files of plugin won't be preseved during update process of plugin. So I recomande you to take backup of your code before to updating your plugin.

    Hope this helps you. Please ping me back here if you need help with anythign else.

    Enjoy WPMU DEV. :slight_smile:

    Cheers,
    Milan

  • janineisabelle

    Milan,

    While I thank you for figuring this out, as I'm positive there will be other people who need to implement this change, you went in and modified core files after I clearly stated the site was going live. I woke up this morning to changes having been made and an email from my designer asking why the styling on the navigation was wrong while students are now coming in and viewing their courses. I don't have time to figure out how to implement the styling changes necessary with the code you rewrote and so had to undo all your changes and go back to the default pagination.

    If you had perhaps let me know yesterday that you were working on this snippet, and sent me the code rather than implementing it yourself, you would have saved me a lot of stress this morning.

  • Milan

    Hello janineisabelle,

    I hope you are having a good day and sorry to hear that changes stressed you out.

    The reason why I made changes was the one you mentioned why I should not. :slight_smile: You said, you site is about to go live and you want that pagination requirement implemented anyhow. So I thought its better to inject code rather than describing how to do here because the way code structured, it was requiring special attention while injecting. But now I got your point that you didn't want that code to be injected. I am sorry that I assumed what you want wrongly. Hope you forgive me. :slight_smile:

    Now if we talk about designing aspect of navigation then, you can see in video that it is designed and rendered well in context of default pagination style. Isn't it ? If there is something about designing aspect of that navigation in which I can help you with, please let me know. I can promise you that this time I want inject my changes to your site without your approval. :slight_smile: Just let me know specifically what you want to tweak and style in those navigation buttons and I will guide you further with this.

    Hope this helps you and thanks for being with us. :slight_smile:

    Wishing you very good day sir.

    Cheers,
    Milan