Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hi,

I'm developing a site using couchcms. We've enabled pretty URLs. That's a great feature. But our post titles long. And with categories and subcategories, permalinks are are quite lengthy. It's an issue when we add social media share buttons. Specially with twitter. Is it possible to integrate a URL shortener addon? With PHP we can use a third party service like goo.gl and make our links short. For an example one can use David Walsh's script.

Or if we can just get the original URL not the pretty version, that would be great. (ie: http://example.com/blog.php?f=3) Is there a way to retrieve it using a shortcode similar to k_page_link? Thanks.
Hi,

If I am not wrong, Twitter does not count a URL's length in its character count. Or am I mistaken?

Anyway, you can use David Walsh's method (or any other library) to create the shortened URLs - Couch *is* a PHP application after all.

If, however, using just the simple native URLs of Couch (i.e. non-prettyURLs) would be sufficient for your usecase, here is a way of doing it.

The cms:link tag we normally use to create links always takes the prettyURLs settings into consideration. So what we'll do is create a new tag that only always returns a simple link.

Please put the following code in your 'couch/addons/kfunctions.php' file (if this file is not present, you'll have to rename the 'kfunctions.example.php' to 'kfunctions.php') -
Code: Select all
// TAG: cms:plain_link
// Get non-prettyURL links of the various views.
// The masterpage should exist to get back a link.
$FUNCS->register_tag( 'plain_link', plain_link_handler );
function plain_link_handler( $params, $node ){
    global $FUNCS, $DB, $PAGE, $CTX;
    if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}

    extract( $FUNCS->get_named_vars(
                array(
                       'masterpage'=>'',
                       'page'=>'',
                       'folder'=>'',
                       'year'=>'',
                       'month'=>'',
                       'day'=>''
                      ),
                $params)
           );

    // sanitize params
    $masterpage = trim( $masterpage );
    if( $masterpage=='' ){ return; } // No masterpage, no link

    $page = trim( $page );
    $folder = trim( $folder );
    $year = trim( $year );
    $month = trim( $month );
    $day = trim( $day );

    if( $page ){
        // page-view
        $sql = "t.id = p.template_id and t.name='" . $DB->sanitize( $masterpage ) . "' and page_name='" . $DB->sanitize( $page ). "'";
        $rs = $DB->select( K_TBL_TEMPLATES . ' t, ' . K_TBL_PAGES . ' p ', array('p.id as pid'), $sql );
        if( count($rs) ){
            $pid = $rs[0]['pid'];

            return K_SITE_URL . $masterpage . '?p=' . $pid;

        }
    }
    elseif( $folder ){
        // folder-view
        $sql = "t.id = f.template_id and t.name='" . $DB->sanitize( $masterpage ) . "' and f.name='" . $DB->sanitize( $folder ). "'";
        $rs = $DB->select( K_TBL_TEMPLATES . ' t, ' . K_TBL_FOLDERS . ' f ', array('f.id as fid'), $sql );
        if( count($rs) ){
            $fid = $rs[0]['fid'];
           
            return K_SITE_URL . $masterpage . '?f=' . $fid;
           
        }
    }
    elseif( $year ){
        // archive-view
        $sql = "t.name='" . $DB->sanitize( $masterpage ) . "'";
        $rs = $DB->select( K_TBL_TEMPLATES . ' t', array('t.id as tid'), $sql );
        if( count($rs) ){
            if( $month ) $month = sprintf( "%02d", $month );
            if( $day ) $day = sprintf( "%02d", $day );
            $link = $masterpage . '?d=' . $year;
            if( $month ) $link .= $month;
            if( $day ) $link .= $day;
               
            return K_SITE_URL . $link;
        }

    }
    else{
        // home-view
        $sql = "t.name='" . $DB->sanitize( $masterpage ) . "'";
        $rs = $DB->select( K_TBL_TEMPLATES . ' t', array('t.id as tid'), $sql );
        if( count($rs) ){
            return K_SITE_URL . $masterpage;
        }
    }

    return;
}

The code above will make available a tag named cms:plain_link that can be used as follows -
Code: Select all
home_view: <cms:plain_link masterpage='tmp.php' /> <br />
page_view: <cms:plain_link masterpage='tmp.php' page='abc' /> <br />
folder_view: <cms:plain_link masterpage='tmp.php' folder='opera' /> <br />
archive_view: <cms:plain_link masterpage='tmp.php' year='2015' /> <br />
archive_view: <cms:plain_link masterpage='tmp.php' year='2015' month='3' /> <br />
archive_view: <cms:plain_link masterpage='tmp.php' year='2015' month='3' day='21' /> <br />

As you can see, it works exactly the way cms:link tag does but always returns a plain URL.

A practical application of it on your site could be to place the following code in a snippet and then embedding the snippet in your template's footer -
Code: Select all
<cms:if k_user_access_level ge '7' >
    <a href="
        <cms:if k_is_home >
            <cms:plain_link masterpage=k_template_name />
        <cms:else_if k_is_folder />
            <cms:plain_link masterpage=k_template_name folder=k_folder_name />
        <cms:else_if k_is_archive />
            <cms:plain_link masterpage=k_template_name year=k_year month=k_month day=k_day />
        <cms:else_if k_is_page />
            <cms:plain_link masterpage=k_template_name page=k_page_name />
        </cms:if>
    ">PLAIN LINK</a>
</cms:if>

This way, Couch will show the plain link of whatever page is being displayed but only to the admins.

You can use the plain link on Twitter etc.
Couch will always redirect the visitor to the prettyURL version of the link so this will have no SEO implications.

Hope it helps.
Great. Thanks for the explanation. This definitely solved my problem.
3 posts Page 1 of 1