We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21606
    • 46 Posts
    Have a client who started a site quite some time ago who didn't use the description content (evo site) on any of their pages and a situation has now created a significant need to use this info for their 'new' site. If I want to use some of the content as a description rather than having them review and populate the description for over 500 pages how can I do that?
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      I believe, the meta description needs to be different for each page, otherwise it loses its SEO point.

      Anyway,
      for doing what you're asking, I would rather create a simple php file that will fill up all the descriptions, by accessing the modx_site_content table directly, and then delete it afterward.
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 21606
        • 46 Posts
        absolutely right. so using that as a basis, there should be a 'formula' that allows me to use the content and a 'limiting' (character limiting) 'formula' to get what we need. let's put it this way, i'd like to grab the first 150 characters of the content and stuff them in the page description (meta description). my attempts so far have been abysmal, got a suggestion? thank you for your quick response and to anyone who has a suggestion.Quote from: goldsky at Nov 14, 2012, 08:53 PM
        I believe, the meta description needs to be different for each page, otherwise it loses its SEO point.

        Anyway,
        for doing what you're asking, I would rather create a simple php file that will fill up all the descriptions, by accessing the modx_site_content table directly, and then delete it afterward.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          That depends on what you are using the description for. You can use a placeholder with a phx modifier in the case of some listing snippets such as Ditto and Jot listing; these contain their own copy of the phx library and do not require the plugin to be installed.

          You could use a snippet to return a sanitized bit of the content.

          Using the PHx plugin, you could use a phx modifier for the [*content*] tag, such as

          [*content:limit=`100`:notags*]


          You can create a custom modifier if you need a different modifier. See http://wiki.modxcms.com/index.php/PHx

          I would be more inclined to write a snippet to pull the desired bit of content from the 'content' field and insert it into the 'description' field in the database for all existing resources, and run that snippet once (create an otherwise empty resource with only the snippet's tags as its content then load the page in your browser to run the snippet; delete or re-use the resource for something else afterwards) to set the value for existing resources. New resources would have their 'description' field filled out by the editing user on creation, and the [*description*] resource variable could be used as usual for all resources.

          This is a crude example, but it only needs to be run once. For so many resources, this should be run during an otherwise quiet time on the site so the 500 or so UPDATE queries won't bother usual visitor usage of the site.
          $sql = 'SELECT id,content FROM modx_site_content';
          $result = mysql_query($sql);
          while ($row = mysql_fetch_assoc($result)) {
              $desc = substr($row['content'], 0, 100);
              // further sanitize the string if necessary to strip tags etc.
              $query = "UPDATE modx_site_content SET description = $desc WHERE id = $row['id']";
              mysql_query($query);
          }
          


            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 11055 ☆ A M B ☆
            • 3,112 Posts
            Untested, but this should work
            Create a PHP file
            <?php
            
            /**
             * Fix HTML tags on the specified characters.
             * @param   string  $text   content
             * @return \tidy
             */
            function properHTML($text) {
                if (!class_exists('tidy')) {
                    return $text;
                }
                // Tidy
                $tidy = new tidy;
            
                /**
                 * Change this
                 * @link    http://tidy.sourceforge.net/docs/quickref.html
                 */
                $tidyConfig = array();
            
                $tidy->parseString($text, $tidyConfig);
                $tidy->cleanRepair();
            
                if ($tidy->errorBuffer) {
                    $errorBuffer = "There are some errors!\n";
                    $errors = explode("\n", $tidy->errorBuffer);
                    foreach ($errors as $error) {
                        $errorBuffer .= $error . "\n";
                    }
                    echo $errorBuffer;
                }
                return $tidy;
            }
            
            ///////////////// CHANGE THIS !!!!!!! ///////////////
            $host = 'localhost';
            $userhost = 'root';
            $passhost = '123';
            
            $mysql = mysqli_connect($host, $userhost, $passhost);
            if (!$mysql) {
                die('<strong>Database connection error: </strong>' . mysqli_error());
            }
            ///////////////// CHANGE THIS !!!!!!! ///////////////
            $db = 'change_with_your_db_name';
            $table = 'modx_site_content';
            
            $select = mysqli_select_db($mysql, $db);
            if (!$select) {
                die('<strong>Can not select database.</strong>');
            }
            
            $query = "SELECT id, description, content from " . $table;
            
            $result = mysqli_query($mysql, $query);
            if (!$result) {
                die('<strong>Query is failed.</strong>');
            }
            
            ///////////////// CHANGE THIS !!!!!!! ///////////////
            // Add more doc's IDs if you want to skip them
            $ignoreIds = array(1);
            
            while ($l = mysqli_fetch_assoc($result)) {
                if (empty($l['description']) &&
                        !in_array($l['id'], $ignoreIds)
                ) {
                    /**
                     * substr: strip by characters
                     */
                    $description = substr($l['content'], 0, 150);
                    $description = properHTML($description);
            
                    mysqli_query($mysql, "UPDATE modx_site_content SET description=$description WHERE id=" . $l['id']);
                }
            }
            
            mysqli_free_result($result);
            mysqli_close($mysql);
            


            What properHTML does is to fix chopped of HTML tags when substr does its work. Just make sure you have PHP's tidy enabled. It will try to enclose missing HTML's pairing tags, eg <a href="...">bla bla [cut here].

            If you don't like substr, you can explode('.', $l['content']) and return 1 or 2 sentences. It doesn't require that properHTML function.

            while ($l = mysqli_fetch_assoc($result)) {
                if (empty($l['description']) &&
                        !in_array($l['id'], $ignoreIds)
                ) {
                    /**
                     * strip by number of sentences
                     */
                    $contents = explode('.', $l['content']);
                    $description = $contents[0] . '.' . $contents[1] . '.';
            
                    mysqli_query($mysql, "UPDATE modx_site_content SET description=$description WHERE id=" . $l['id']);
                }
            }


            After that, delete again the file, and check the result. [ed. note: goldsky last edited this post 13 years, 11 months ago.]
              Rico
              Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
              MODx is great, but knowing how to use it well makes it perfect!

              www.virtudraft.com

              Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

              Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

              Maintainter/contributor of Babel

              Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
              • 21606
              • 46 Posts
              Wow! Thanks guys - I'll give this a shot.