Forums / General Questions / Meta Tags for News Box Manager pages?

Meta Tags for News Box Manager pages?

Views: 4,341

Results 1 to 8 of 8
8 Mar 2017, 1:05 PM
#1
bonheddwr avatar

bonheddwr

Zen Follower

Join Date:
Jun 2007
Location:
Cymru
Posts:
124
Plugin Contributions:
0

Meta Tags for News Box Manager pages?

I currently use the News Box Manager plugin on our site, It works exactly how I want, but unfortunately at the moment there is no way of including specific meta tags for the different news stories via the plugin. I have asked the plugin support thread, and this is something that they are thinking of including in future.

I have used the metatags.php file to add specific meta tags to different permanent pages and EZ pages using this easy to follow guide, but is there a way I can use metatags.php so that each individual news story have different meta tags. Please view below the structure. Each differnet ID number is a different news item.

https://www.welsh-lovespoons.co.uk/index.php?main_page=more_news&news_id=5

I would appreciate any comments / advice.

10 Mar 2017, 2:31 PM
#2
bonheddwr avatar

bonheddwr

Zen Follower

Join Date:
Jun 2007
Location:
Cymru
Posts:
124
Plugin Contributions:
0

Re: Meta Tags for News Box Manager pages?

I forgot to include a link to the useful page explaining meta tags - https://www.zen-cart.com/content.php?104

I can add the following to the metatags.php file, but this gives EVERY news item exactly the same meta tags:

define('META_TAG_DESCRIPTION_MORE_NEWS','');
define('META_TAG_KEYWORDS_MORE_NEWS','');

Is there a way I can set different meta tags for the different 'news_id='?

This is possible with EZpages, you simply use the following and change the # for the specific EZpage id.

define('META_TAG_DESCRIPTION_EZPAGE_#','');
define('META_TAG_KEYWORDS_EZPAGE_#','');

I would be very grateful if anyone can help on this matter

10 Mar 2017, 7:15 PM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Meta Tags for News Box Manager pages?

For v1.5.5, you'll see in the /includes/modules/meta_tags.php file that it starts out with some basic setup, then fires off a massive switch() {} statement, in which it tests several situations (case() statements) (of which most are just "what kind of page are we on?") and builds the relevant metatag variables based on the specified situation. And then at the very bottom it takes those variables and does the necessary define()ing so the values can be used by the template.

Prior to v1.5.5 all the define() activity was done inside each case(), instead of at the end of the file. This proved to be too inflexible, so in v1.5.5 it was streamlined a bit.

In fact, since v1.5.5 there's also an observer/notifier hook near the end of the file, above the define() statements, which you could use to put your custom New Box Manager stuff into a separate observer class, and thus not have to touch the meta_tags.php module file at all.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Mar 2017, 6:13 PM
#4
bonheddwr avatar

bonheddwr

Zen Follower

Join Date:
Jun 2007
Location:
Cymru
Posts:
124
Plugin Contributions:
0

Re: Meta Tags for News Box Manager pages?

Thank you very much for responding. I am currently using version v1.5.5 of Zen Cart. I'm afraid that this goes way beyond my knowledge of coding, therefore I will need to wait until the News Box manager plugin is updated. From what I understand they are considering adding this feature. Thanks again.

15 Mar 2017, 6:44 PM
#5
bonheddwr avatar

bonheddwr

Zen Follower

Join Date:
Jun 2007
Location:
Cymru
Posts:
124
Plugin Contributions:
0

Re: Meta Tags for News Box Manager pages?

Sorry, one more question! For pages that do not have specific meta tags assigned individually to them, shouldn't the description and keywords be generated automatically from the site wide meta tags set in /includes/languages/english/YOUR_TEMPLATE_NAME/meta_tags.php?

This doesn't happen with my site. Every page uses a list of our website categories as the keywords and description if I haven't chosen specific ones for that page in the meta_tags.php file. For example, the automatically generated description and keyword meta tags for the following two pages:

https://www.welsh-lovespoons.co.uk/index.php?main_page=more_news&news_id=5
https://www.welsh-lovespoons.co.uk/index.php?main_page=gv_faq

is:

<meta name="keywords" content="Birthday Gifts Valentines Day Gifts Mothers' Day Gifts Christmas Gifts Wedding Favours Rhiannon Jewellery Christening Gifts Christian Gifts New Baby Gifts Engagement Gifts Anniversary Gifts Wedding Day Gifts FULL RANGE OF SPOONS 5th Anniversary Gifts welsh love spoons, lovespoons, love spoons, love spoon, lovespoon, welsh lovespoons, celtic lovespoon, celtic love spoon, cadwyn News" />
<meta name="description" content="Welsh Love Spoons : News - Birthday Gifts Valentines Day Gifts Mothers' Day Gifts Christmas Gifts Wedding Favours Rhiannon Jewellery Christening Gifts Christian Gifts New Baby Gifts Engagement Gifts Anniversary Gifts Wedding Day Gifts FULL RANGE OF SPOONS 5th Anniversary Gifts welsh love spoons, lovespoons, love spoons, love spoon, lovespoon, welsh lovespoons, celtic lovespoon, celtic love spoon, cadwyn" /
```>

and

<meta name="keywords" content="Birthday Gifts Valentines Day Gifts Mothers' Day Gifts Christmas Gifts Wedding Favours Rhiannon Jewellery Christening Gifts Christian Gifts New Baby Gifts Engagement Gifts Anniversary Gifts Wedding Day Gifts FULL RANGE OF SPOONS 5th Anniversary Gifts welsh love spoons, lovespoons, love spoons, love spoon, lovespoon, welsh lovespoons, celtic lovespoon, celtic love spoon, cadwyn Gift Certificate FAQ" /> <meta name="description" content="Welsh Love Spoons : Gift Certificate FAQ - Birthday Gifts Valentines Day Gifts Mothers' Day Gifts Christmas Gifts Wedding Favours Rhiannon Jewellery Christening Gifts Christian Gifts New Baby Gifts Engagement Gifts Anniversary Gifts Wedding Day Gifts FULL RANGE OF SPOONS 5th Anniversary Gifts welsh love spoons, lovespoons, love spoons, love spoon, lovespoon, welsh lovespoons, celtic lovespoon, celtic love spoon, cadwyn" /> ```

Is this the correct way Zen Cart should be generating descriptions and Keywords for my site? Is there a way that the description at least is automatically generated from the page content, not the categories (if no manual meta tags have been assigned)?

I hope this makes sense!

22 Mar 2017, 2:59 AM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Meta Tags for News Box Manager pages?

The following custom observer class may be of help. In theory it could be expanded to support other content types too.
However, it's based on code, not tested on a live site (I don't have the news box manager installed.)

I've highlighted in red the sections that are specific to the New Box Manager plugin. The rest is generic (but required).

It should be uploaded to /includes/classes/observers/auto.news_box_manager_metatags.php
It depends on Zen Cart v1.5.5

<?php
/**
 * Adds metatags for news box manager plugin
 * This module observer class is designed for Zen Cart v1.5.5 minimum.
 */

class zcObserver[B]NewsBoxManagerMetatags[/B] extends base
{
    function __construct()
    {
        /**
         * List the pages you want to override metatags for.  Example: if main_page=freddy then enter 'freddy' below.
         */
        $this->pages_to_override = array(
          [B]  'more_news',
            'news_archive',[/B]
            );

        /**
         * Do not edit this list of attach hooks
         */
        $this->attach($this, [
            'NOTIFY_MODULE_START_META_TAGS',
            'NOTIFY_MODULE_META_TAGS_BUILDKEYWORDS',
            'NOTIFY_MODULE_META_TAGS_UNSPECIFIEDPAGE',
            'NOTIFY_MODULE_META_TAGS_OVERRIDE',
            'NOTIFY_MODULE_END_META_TAGS',
        ]);
    }

    /**
     * Here we read $current_page, determine whether you want to override the usual generation of metatags or not, and set $meta_tags_over_ride accordingly
     * When using this observer to "add" metatags for pages not already in Zen Cart, you WILL want to set $meta_tags_over_ride to true, else the 'default' will kick in.
     */
    function updateNotifyModuleStartMetaTags(&$class, $eventID, $current_page, &$metatag_page_name, &$meta_tags_over_ride)
    {
        if (in_array($current_page, $this->pages_to_override)) $meta_tags_over_ride = true;

        // if we wanted to point the metatags module to treat $current_page as some other page_name then we can set $metatag_page_name to that other page, and then we're done with it.
    }

    /**
     * If we want to build specific keywords for this page, we do those here.
     * We could pull them from database records, or language defines, etc.
     */
    function updateNotifyModuleMetaTagsBuildKeywords(&$class, $eventID, $defined_custom_keywords, &$keywords_string_metatags)
    {
        global $db;

    }

    /**
     * This fires in the middle of handling the "default" output if no page-specific handling was found in the metatags module. 
     * If you leave $meta_tags_over_ride as false here then this function is ignored. 
     * You CAN set $meta_tags_over_ride to true, perhaps based on the content of any of the other variables that have been passed in to this function.
     */
    function updateNotifyModuleMetaTagsUnspecifiedpage(&$class, $eventID, $current_page, &$metatag_page_name, &$meta_tags_over_ride, &$metatags_title, &$metatags_description, &$metatags_keywords)
    {
        global $db;
        if ($meta_tags_over_ride === false) return;

    }

    /**
     * If this Observer class is being used to provide metatags for pages not already handled by Zen Cart core code, this is where you calculate that output.
     * Chances are you will be pulling data from the database or from language defines.
     */
    function updateNotifyModuleMetaTagsOverride(&$class, $eventID, $metatag_page_name, &$meta_tags_over_ride, &$metatags_title, &$metatags_description, &$metatags_keywords)
    {
        global $db;
        // if we haven't already set $meta_tags_over_ride to true, then we MUST do that, else the changes made in this function will be of no effect

        if ($meta_tags_over_ride === false) return;

        // we can retrieve data or language defines and use those to generate $metatags_title, $metatags_description, $metatags_keywords based on $metatag_page_name

        if (in_array($metatag_page_name, $this->pages_to_override)) {
[B]            /**
             * News box manager
             * Since header_php.php has already run, we have access to the following variables, if we "global" them:
             * $news_id
             * $languages_id
             * $news_title
             * $news_content
             * $start_date
             * $end_date (after outputting NEWS_DATE_SEPARATOR)
             */
            global $news_id, $languages_id, $news_title, $news_content, $start_date, $end_date;

            $metatags_title = $news_title . ' - ' . $start_date;
            $metatags_description = zen_trunc_string(zen_clean_html($news_content), 250, false);
            $metatags_keywords = zen_trunc_string(zen_clean_html($news_content), 100, false);[/B]
        }

    }
    function updateNotifyModuleEndMetatags(&$class, $eventID)
    {

    }

}

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

22 Mar 2017, 11:40 AM
#7
bonheddwr avatar

bonheddwr

Zen Follower

Join Date:
Jun 2007
Location:
Cymru
Posts:
124
Plugin Contributions:
0

Re: Meta Tags for News Box Manager pages?

Thank you so much for your help. I don't know where you get the time! I copied the code and saved as auto.news_box_manager_metatags.php and uploaded the file to /includes/classes/observers/ via FileZilla (see image)

Attachment 16991

Is there anything else I should do? Do I need to make changes to /includes/modules/meta_tags.php or /includes/languages/english/YOUR_TEMPLATE_NAME/meta_tags.php? I cannot see any difference at the moment. Many Thanks again.

Thanks so much again for your help. Hedd

23 Mar 2017, 12:16 PM
#8
bonheddwr avatar

bonheddwr

Zen Follower

Join Date:
Jun 2007
Location:
Cymru
Posts:
124
Plugin Contributions:
0

Re: Meta Tags for News Box Manager pages?

DrByte:

The following custom observer class may be of help. In theory it could be expanded to support other content types too.
However, it's based on code, not tested on a live site (I don't have the news box manager installed.)

I've highlighted in red the sections that are specific to the New Box Manager plugin. The rest is generic (but required).

It should be uploaded to /includes/classes/observers/auto.news_box_manager_metatags.php
It depends on Zen Cart v1.5.5

I tried the above method (see my message above) but unfortunately the meta tags for news stories stayed the same (list of categories). I have also tried the following by trying to copy the way of creating meta tags for EZPages, but couldn't get this to work either. I am a novice and there must be errors in the code (if it makes sense at all!)

In /includes/modules/meta_tags.php I added the following under the code regarding EZPages:

// EZ-Pages:
  case 'page':
  $ezpage_id = (int)$_GET['id'];
  $chapter_id = (int)$_GET['chapter'];
  if (defined('META_TAG_TITLE_EZPAGE_'.$ezpage_id)) define('META_TAG_TITLE', constant('META_TAG_TITLE_EZPAGE_'.$ezpage_id));
  if (defined('META_TAG_DESCRIPTION_EZPAGE_'.$ezpage_id)) define('META_TAG_DESCRIPTION', constant('META_TAG_DESCRIPTION_EZPAGE_'.$ezpage_id));
  if (defined('META_TAG_KEYWORDS_EZPAGE_'.$ezpage_id)) define('META_TAG_KEYWORDS', constant('META_TAG_KEYWORDS_EZPAGE_'.$ezpage_id));
// News-Pages:
  case 'more_news':
  $news_id = (int)$_GET['news_id'];
  if (defined('META_TAG_TITLE_NEWSPAGE_'.$news_id)) define('META_TAG_TITLE', constant('META_TAG_TITLE_NEWSPAGE_'.$news_id));
  if (defined('META_TAG_DESCRIPTION_NEWSPAGE_'.$news_id)) define('META_TAG_DESCRIPTION', constant('META_TAG_DESCRIPTION_NEWSPAGE_'.$news_id));
  if (defined('META_TAG_KEYWORDS_NEWSPAGE_'.$news_id)) define('META_TAG_KEYWORDS', constant('META_TAG_KEYWORDS_NEWSPAGE_'.$news_id));

In /includes/languages/english/YOUR_TEMPLATE_NAME/meta_tags.php I added the following under the code regarding EZPages and tested it for one of the news items news_id=5 but with no luck :/ :

// News-Pages meta-tags.  Follow this pattern for all news-pages for which you desire custom metatags. Replace the # with news id.
// If you wish to use defaults for any of the 3 items for a given page, simply do not define it.
// (ie: the Title tag is best not set, so that site-wide defaults can be used.)
// repeat pattern as necessary
  define('META_TAG_DESCRIPTION_NEWSPAGE_5','Prawf');
  define('META_TAG_KEYWORDS_NEWSPAGE_5','Prawf');
  define('META_TAG_TITLE_NEWSPAGE_5', 'Prawf');