Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
Hi,

I try to make a custom tag that scans the page for some SEO mistakes. So what I did for now is:

- made a folder called SEO in couch/addons
- downloaded simple dom parser https://github.com/sunra/php-simple-html-dom-parser into the SEO folder
- created a file called simple_seo.php in the same folder
- added to the kfunctions file this: require_once( K_COUCH_DIR.'addons/SEO/simple_seo.php' );

The code of my simple_seo.php is:

Code: Select all
<?php


if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly

include('simple_html_dom.php');


class SimpleSeo{


        function simple_seo_handler( $params, $node ){
            global $FUNCS, $CTX;
            if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}
           
            $url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
             
            extract( $FUNCS->get_named_vars(
                array(
                       'url' => $url , // URL  Default is current page               
                      ),
                $params)
            );
           
            // try to load a simple seo overview of the page
           
            $simplehtml = file_get_html($url);
           

            $pattern = '/ |,/i';    // ' ' or ','
            $replpatt = '/(?<!\S)\S{1}(?!\S)/i'; // not sure about this
           
            // my attempt to get a clean word count of the title tag

            echo '<b>Page : </b>' . $url . '<hr>';

            foreach($simplehtml->find('title') as $meta_title)

             {   
               echo '<b>Title : </b>' . $meta_title->innertext . '<br>';
               echo '<small>Best Practices : about 50-70 characters , unique and use of important keywords.</small>' . '<br>';
   
               $str = "$meta_title->innertext";
               $str = trim(preg_replace("$replpatt", " ", $str));  // remove one-letter words
               $str_word_count = count(preg_split($pattern, $str, -1, PREG_SPLIT_NO_EMPTY));
               echo 'Your title tag has '. $str_word_count. ' words and a total of ';
               echo strlen($str) . ' characters' . '<hr>';
               }
   
              if (!isset($meta_title)) { echo '<strong>No title found!</strong> please insert one title tag with a text about 50-70 characters between your head tags' . '<hr>'; }
   
            // my attempt to get a clean word count of the meta descripton tag   
   
   
            $meta_description = $simplehtml->find("meta[name='description']", 0)->content;
            if (isset($meta_description))
            {
            echo '<b>Description : </b>' .$meta_description .'<br>';
            echo '<small>Best Practices : about 170 characters , unique , well written and use of important keywords.</small>' . '<br>';
   
            $strD = $meta_description ;
            $strD = trim(preg_replace("$replpatt", " ", $strD));  // remove one-letter words
            $str_word_count = count(preg_split($pattern, $strD, -1, PREG_SPLIT_NO_EMPTY));
            echo 'Your meta description tag has '. $str_word_count. ' words and a total of ';
            echo strlen($strD) . ' characters' . '<hr>';
            }
   
            if (!isset($meta_description)) { echo '<strong>Description not found!</strong> please insert one meta tag named description with a text of 170 characters between your head tags' . '<hr>'; }
   
            //word count of the meta keyword tag
           
            $meta_keywords = $simplehtml->find("meta[name='keywords']", 0)->content;
            if (isset($meta_keywords))
            {
            echo '<b>Keywords : </b>' .$meta_keywords.'<br>';
           
            $strK = $meta_keywords ;
            $str_word_count = count(preg_split($pattern, $strK, -1, PREG_SPLIT_NO_EMPTY));
            echo 'This page has '. $str_word_count. ' keywords <br>';
            echo '<small>Keywords are not that important anymore but still use them to reflect the main topic of your page</small>' . '<hr>';
            }
           
           
            //some basic checks
           
            $meta_robots = $simplehtml->find("meta[name='robots']", 0)->content;
            if (isset($meta_robots)) {echo '<b>Robots : </b>' .$meta_robots.'<hr>';}
            if (!isset($meta_robots)) { echo '<strong>No meta robots found!</strong> please insert one between your head tags' . '<hr>'; }
           
           
            foreach($simplehtml->find('h1') as $h)
            {
            echo '<b>h1 : </b>' . $h->innertext .'<br>';
            }
            if (!isset($h)) { echo '<strong>No H1 tag used!</strong> you should reconsider this' . '<hr>'; }
           
            echo '<table style="width:100%">';
            echo '<caption>Images</caption>';
            echo '<tr><th>Image</th><th>Alt <small>(if empty please adjuist)</small></th></tr>';
            foreach($simplehtml->find('img') as $alttags)
            {
            echo'<tr>';
            echo '<td>' . $alttags->src . '</td>';
            echo '<td>' . $alttags->alt .'</td>';
            echo'</tr>';
            }
            echo'</table>';
           
           
           
            //check for top 10 words used in keywords, title and description
            $words = $strK. $strD. $str ;
            $mostused = str_word_count($words, 1);
           
            //try to remove short words. I have some errors here on characters like ä
           
              $array_filter = $mostused ;

              function remove_short_words($word) {
                return strlen($word) > 3;
              }

            $mostused = array_filter($array_filter, "remove_short_words");  //filter the array with function above         
            $usedcount = array_count_values($mostused);
            arsort($usedcount);
            $usedten =(array_slice($usedcount, 0, 10));

            echo '<table style="width:100%">';
            echo '<caption>The words below should reflect the page content</caption>';
            echo '<tr><th>Top ten words</th><th>Times used </th><th>In description </th><th>In keywords </th><th>In Title </th><th>In H1 </th></tr>';
            foreach($usedten as $topten=> $value)
            {
            echo'<tr>';
           
            echo '<td>' . $topten .'</td>'; //errors here on the format of characters like ä
            echo '<td>' . $value .'</td>';
            if (strpos($strD, $topten) !== false)
            echo '<td> Yes </td>'; else echo '<td>  </td>' ;
            if (strpos($strK, $topten) !== false)
            echo '<td> Yes </td>'; else echo '<td>  </td>' ;
            if (strpos($str, $topten) !== false)
            echo '<td> Yes </td>'; else echo '<td>  </td>' ;
            if (strpos($h1, $topten) !== false)
            echo '<td> Yes </td>'; else echo '<td>  </td>' ;

            echo'</tr>';

            }
            echo'</table>';
                       
            // clear the requested page

            $simplehtml->clear();
            unset($simplehtml);
           
         }

}


$FUNCS->register_tag( 'quickseo', array('SimpleSeo', 'simple_seo_handler') );



Now when I use my custom tag <cms:quickseo /> or <cms:quickseo url='some url'/> it seems to work. Only somehow my tag only renders on the top of my page ... it will not show up on the place I put the tag.

Has someone any ideas where I messed up ?
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Hi,

Tag handlers should always return back text and not echo out the output themselves (which is what your code is doing and hence the problem).

For example, following is the way most tags work -
Code: Select all
function sample_tag_handler( $params, $node ){

    $html .= 'Hello';
    $html .= ' World';

    return $html;
}

If you have to incorporate some pre-written code that makes use of echo statements (as in your code), you should wrap that output into a buffer and then return back the buffer's contents, for example as following -
Code: Select all
function sample_tag_handler( $params, $node ){
    ob_start();

    /*  start existing code */
    echo( 'Hello' );
    echo( ' World' );
    /* end existing code */

    $html .= ob_get_contents();
    ob_end_clean();

    return $html;
}

Please use the above mentioned technique to buffer your tag's output and return back text and that should solve the problem.

Hope it helps.
@KK

Thanks ! I have got it working now.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
3 posts Page 1 of 1
cron