Programová úprava obsahu stránky – filtr the_content

Jeden klient potřeboval na určité místo v obsahu vložit reklamní bannery. Od jiného programátora WordPress dostal informaci, že to bohužel není možné.

Požádal jsem ho proto, aby mi poslal zadání a přístupy. Řešení bylo jednoduché: filtr the_content. Při použití toho filtru můžete libovolně měnit samotný obsah stránky/příspěvku.

Výsledný kód pak vypadal cca takto:

[php]add_filter(‚the_content‘,’stk_ads‘);
function stk_ads($content) {
$first_ad = ‚This was the first ad code‘;

$second_ad = ‚This was the second ad code‘;

// Insert code after specified element, in this case <blockquote>, using custom fuction
$content = str_insert($content,'<blockquote>‘,$first_ad);

// Insert code before specified element, in this case <ifr, using custom fuction
$content = str_insert_before($content,<ifr‘,$second_ad);

return $content;
}[/php]

Přidat komentář