• Moderator bcworkz

    (@bcworkz)


    [Moderator Note: This topic was split off from https://www.ads-software.com/support/topic/can-i-use-a-filter-hook-to-modify-the-contents-of-the-section/ ]

    Hey guys, on single posts and pages there should already be a canonical meta link in your head section. You don’t want to add another one. It defeats the purpose and makes for bad SEO if they are different.

    We can alter the canonical link that is already there with the “get_canonical_url” filter. You filter callback is passed the URL WP wants to use, plus the post object being displayed. You can get the post date from the post object and alter the URL when required. From what I gather, the cutoff is the date when the old HTTP URL was no longer valid. If you don’t recall when that happened, perhaps looking at your SSL certificate’s expiration date will help. Work backwards based on how long the certification was supposed to be good for.

    How you would selectively alter the OpenGraph tag depends on how it is generated in the first place. Since OG tags are not part of the default WP distribution, I cannot comment on that aspect.

    The above sort of filter code is usually added to a custom child theme’s functions.php. In the case of a commercial child theme based on a framework, this is not a good choice. However, this code works equally well from a custom site specific plugin. You can place any other custom code you might want to create in this same plugin.

    @davidborrink — it appears part of you post was cut off for some reason. If I haven’t completely answered your questions, go ahead and ask.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thanks, bcworkz for responding.

    So is “cutoff_date” a given variable in the process of a post or page being called up?

    I assume this would need to filter something in the loop since it needs to happen according to content in either a page or a post. Which hook would I need to filter in order to make this happen?

    Moderator bcworkz

    (@bcworkz)

    The cut off date is a value that’s the same for the entire site. The OP hard coded it in their filter callback. This is OK since it’s specific to their site and it never would change. One could store the value in the options table if one is morally opposed to hard coded values. It is compared against the publish date of the current post to decide which protocol to use in particular links.

    The OP provided an example that filters certain links in post content and excerpts just before they are output in a loop. You would do something similar where ever the links are output. Without knowing where or how your links are output, I cannot suggest exactly how they would be filtered.

    You might consider creating a custom template tag function to handle the process if links are output from a template where no filter mechanism is available. It can only work in relation to a particular post though, since posts are the only object where a specific publish date is available. In any case, most WP functions are filterable, so there may not be a need for this template tag.

    Okay, well my cut off date when I switched to https was a couple weeks ago, so obviously I need to tell the site to use that date as the cutoff in order to have a post determine whether to go http or https. How will this code know that date?

    if post_date < cutoff_date
        <link href="https://www.example.com/REQUEST_URI" rel="canonical" />
    else
        <link href="https://www.example.com/REQUEST_URI" rel="canonical" />
    endif

    And you say that most posts are the only date-sensitive content, so that’s fine. Are you saying that I can filter this in a template tag of my choice within the loop so that it happens somewhere in the process of rendering a post?

    • This reply was modified 7 years, 11 months ago by David Borrink.
    Moderator bcworkz

    (@bcworkz)

    Not exactly. A custom template tag is another option if there’s no filter to use for the link output. This would replace an existing template tag that has no filter. It does mean editing the template itself as well. You could just paste the actual code onto the template, but if there are several templates to edit, having a simple function call is a cleaner option.

    You should know that Curdin’s suggested “code” is not proper PHP syntax. It’s not unusual for coders to use “pseudocode” to illustrate a concept without confusing syntax getting in the way.

    The OP’s code will take care of links in post content and excerpts. As I said, another filter will handle canonical links. What else you do depends on what links you need to modify.

    There’s a number of ways to tell the site what cutoff date to use. I think the simplest would be to define a constant in one place, then use that everywhere a date comparison is made.
    define('CUTOFF', strtotime('2017-03-27'));

    Then a simpler syntactically correct date comparison could be made:
    if ( get_the_date('U') < CUTOFF ) {

    Okay, I’d vastly prefer doing this as a function, then it’s global that way. I’m using the Genesis framework so I definitely would prefer the hook method. It would appear according to you that I could create a hook that filters the get_the_date while it runs through the post. Will that put the correct url in my og:url meta tag in my head for a given post? If I understand, that’s what calls up the social media numbers.

    Is this a wp-config.php statement, or a function: define('CUTOFF', strtotime('2017-03-27'));

    Moderator bcworkz

    (@bcworkz)

    The constant definition line is not part of a function because it can only be defined once. The define() itself is a function call, but I don’t think that’s what you meant. It can go on wp-config.php, or it can go on another code page, as long as it’s defined before it’s used and the code page is loaded once per request.

    I prefer to define my constants at the beginning of the extension I’m coding. The top of functions.php if a child theme, or the main plugin page for a plugin. This keeps all my code in one group instead of scattered about the site.

    > “I could create a hook that filters the get_the_date”
    I’m not sure if you are really misunderstanding me or just using poor terminology. So I’ll say no. There is a filter hook where you can alter post content called “the_content”. The callback function you add to “the_content” could use get_the_date() in part to decide if a change should be applied or not.

    “the_content” filter can be used to alter data-href attributes that occur in post content. This will not affect OpenGraph tags in the head section. There is no way for “the_content” filter to do that, it works on the wrong data for that.

    Unfortunately, I do not know how OG tags are placed in head to begin with, other than it’s possibly through a callback added to the “wp_head” action. The ideal approach is to locate the code that inserts the og:url meta tag. Hopefully it will have a filter hook through which you can alter the URL that is output.

    If it does not, the preferred solution would be to customize the function by copying it to your own code extension and giving it a new name. Edit as necessary. Then additional code is needed to remove the original function from the “wp_head” action and add your version instead.

    The other “dirty” option would be to directly hack the code generating the og:url meta tag. The problem with this is when the original code is updated, your modification will be lost and you’ll need to reapply the change after every update. It would be a dangerous idea to avoid updates just to avoid having to reapply your changes because updates could include important security patches.

    Moderator bcworkz

    (@bcworkz)

    @davidborrink — I split most of our conversation off from the topic you originally posted to. The OP and any subscribers may be getting annoyed with continued e-mail notifications. You’ll want to subscribe to this new topic in order to continue getting notifications. The new link:
    https://www.ads-software.com/support/topic/split-can-i-use-a-filter-hook-to-modify-the-contents-of-the-section/

    bc, thanks for notifying me of that. I went to the other post and thought “um, it’s gone!”

    I see you got all the relevant topic tags on this one, too.

    I just read your last tech comment above. Yikes. So getting the OpenGraph tags to change will have nothing to do with any filtering of content. Okay.

    This business about losing the social media numbers due to a switch to https has the tendency to look like an unsolvable activity. I’m surprised that a consequence of taking the smart move of securing our sites has the unfortunate and seemingly unresolvable issue of losing our hard-earned social media numbers. I’ve been reading for a couple weeks and am only finding bits and pieces of people trying and failing to get solutions for this.

    It seems like there ought to be a way to collect both the numbers from a http and https url of a site/post from the social media companies, and then merge them into a number that still shows up in the share totals.

    I’ll check into how my tags are being inserted and see what I can find.

    Moderator bcworkz

    (@bcworkz)

    a consequence of taking the smart move of securing our sites has the unfortunate … issue of losing our hard-earned social media numbers.

    Well said! It’s not really unsolvable if these conditional link fixes work out, though it takes a good coder to solve how to do it properly. The problem really falls to the social media platforms. Their apparent lack of concern for this very real need is profoundly disturbing. Their weak “solution” is to ask site owner’s to implement some silly scheme that plays with protocols. Not all site owners are capable of implementing such changes without hiring someone at a not insubstantial cost. Pathetic!

    As a moderator, I better disclaim that the above is my own personal opinion and does not in any way reflect any policies or standing of the WordPress organization.

    If you cannot find a clean way to alter these elements properly, you may need to resort to a dirty hack of altering the provided code directly. Hopefully the situation will improve before an update wipes out your changes.

    Well, thank you for the compliment on my opinion. We should be able to have our cake and eat it, too, because…. that’s what you do with cake! LOL The more I read about all this, the more I was seeing people express frustration for well over a year about this. This has been a problem for quite a while, apparently. It seems that most are just having to “live with it” and start over. That’s a hard pill to swallow when you’ve enjoyed having some posts with nice numbers to look very credible.

    Yes, you say it right that it really does fall on the social media platforms. Unfortunately I’m at “code geek wannabe” stage, learning more and more all the time, but dirty hacking of the provided code is not at my pay grade yet.

    • This reply was modified 7 years, 11 months ago by David Borrink.
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Split: Can I use a filter hook to modify the contents of the section?’ is closed to new replies.