{"id":3609,"date":"2020-12-30T17:40:29","date_gmt":"2020-12-30T16:40:29","guid":{"rendered":"https:\/\/www.spinat.ca\/?p=3609"},"modified":"2020-12-30T17:43:01","modified_gmt":"2020-12-30T16:43:01","slug":"hook-wordpress-that-triggers-twice","status":"publish","type":"post","link":"https:\/\/www.spinat.ca\/en\/hook-wordpress-that-triggers-twice\/","title":{"rendered":"Hook WordPress that triggers twice !?"},"content":{"rendered":"<p>The job of web developer is sometimes complicated!<\/p>\n<p>We sometimes come across very strange bugs on which we spend hours or even days and which can call into question the whole profitability of the project.<\/p>\n<p>My latest is a WordPress <a href=\"https:\/\/www.spinat.ca\/en\/definition\/hook\/\" data-internallinksmanager029f6b8e52c=\"4\" title=\"D\u00e9finition Hook Wordpress\">hook<\/a> that was triggered twice. Here is the hook in question:<\/p>\n<pre class=\"prettyprint\">add_action( 'wp', 'onload' );\r\nfunction onload()\r\n{\r\n    if ('video' === get_post_type() &amp;&amp; is_singular()){\r\n     \/\/ Adding an entry in my database\r\n    \/\/ ...\r\n    }\r\n}<\/pre>\n<p>This code, very simple, had to add a database entry.<\/p>\n<p>It worked very well EXCEPT that from time to time it made TWO records.<\/p>\n<p>After long hours of research, debugging (and depression), I was able to find the origin of the problem:<\/p>\n<pre class=\"prettyprint\">&lt;link rel='prev' title='My previous video' href='http:\/\/localhost\/****\/video\/ma-video-precedente\/' \/&gt;\r\n&lt;link rel='next' title='My next video' href='http:\/\/localhost\/****\/video\/ma-video-suivant\/' \/&gt;\r\n<\/pre>\n<p>WordPress defines by default these two tags to define a link between the contents. The rel=\u201dprev\u201d and rel=\u201dnext\u201d tags appeared in 2011 and allow to facilitate the crawl of Google bot on the contents. Some browsers (Chrome, Firefox\u2026) use it to increase the browsing speed between pages by preloading the linked contents.<\/p>\n<p>That\u2019s why my Hook was triggered twice!<\/p>\n<h2>How to avoid that the WordPress hook is triggered twice?<\/h2>\n<p>Just add this in your functions.php file<\/p>\n<pre class=\"prettyprint\">remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );<\/pre>\n<p>I hope I\u2019ve saved you all the time I lost \ud83d\ude42<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>The job of web developer is sometimes complicated! We sometimes come across very strange bugs on which we spend hours or even days and which&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1303,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[115],"tags":[],"_links":{"self":[{"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/posts\/3609"}],"collection":[{"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/comments?post=3609"}],"version-history":[{"count":3,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/posts\/3609\/revisions"}],"predecessor-version":[{"id":3612,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/posts\/3609\/revisions\/3612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/media\/1303"}],"wp:attachment":[{"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/media?parent=3609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/categories?post=3609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spinat.ca\/en\/wp-json\/wp\/v2\/tags?post=3609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}