Hướng dẫn dùng meta property trong PHP

Tôi biết bạn đã tìm ra giải pháp, nhưng điều này có thể giúp bất kỳ ai khác đang tìm kiếm điều tương tự!

Nội dung chính

  • 1. Thêm code vào thẻ head là gì?
  • 2. Cách chèn code, meta tag, script vào thẻ head trong WordPress mới nhất
  • 2.1 Dùng Plugin Insert Headers and Footers
  • 2.2 Sửa giao diện
  • 2.3 Sửa file header.php
  • 2.4 Thêm bằng plugin Elementor
  • 2.5 Thêm bằng code WordPress
  • 2.6 Thêm bằng theme Flatsome
  • 3. Khi nào cần thêm thẻ meta, js vào thẻ head?

Tôi đã sửa đổi một lớp mà tôi tìm thấy ở đây (cảm ơn debers ).

Và tất cả các tham chiếu đến thẻ IPTC có thể được đọc từ tệp PDF này

Và bây giờ là đoạn mã (PHP> = 5,4):

<?
define("IPTC_OBJECT_NAME", "005");
define("IPTC_EDIT_STATUS", "007");
define("IPTC_PRIORITY", "010");
define("IPTC_CATEGORY", "015");
define("IPTC_SUPPLEMENTAL_CATEGORY", "020");
define("IPTC_FIXTURE_IDENTIFIER", "022");
define("IPTC_KEYWORDS", "025");
define("IPTC_RELEASE_DATE", "030");
define("IPTC_RELEASE_TIME", "035");
define("IPTC_SPECIAL_INSTRUCTIONS", "040");
define("IPTC_REFERENCE_SERVICE", "045");
define("IPTC_REFERENCE_DATE", "047");
define("IPTC_REFERENCE_NUMBER", "050");
define("IPTC_CREATED_DATE", "055");
define("IPTC_CREATED_TIME", "060");
define("IPTC_ORIGINATING_PROGRAM", "065");
define("IPTC_PROGRAM_VERSION", "070");
define("IPTC_OBJECT_CYCLE", "075");
define("IPTC_BYLINE", "080");
define("IPTC_BYLINE_TITLE", "085");
define("IPTC_CITY", "090");
define("IPTC_PROVINCE_STATE", "095");
define("IPTC_COUNTRY_CODE", "100");
define("IPTC_COUNTRY", "101");
define("IPTC_ORIGINAL_TRANSMISSION_REFERENCE", "103");
define("IPTC_HEADLINE", "105");
define("IPTC_CREDIT", "110");
define("IPTC_SOURCE", "115");
define("IPTC_COPYRIGHT_STRING", "116");
define("IPTC_CAPTION", "120");
define("IPTC_LOCAL_CAPTION", "121");

class IPTC
{
    var $meta = [];
    var $file = null;

    function __construct($filename)
    {
        $info = null;

        $size = getimagesize($filename, $info);

        if(isset($info["APP13"])) $this->meta = iptcparse($info["APP13"]);

        $this->file = $filename;
    }

    function getValue($tag)
    {
        return isset($this->meta["2#$tag"]) ? $this->meta["2#$tag"][0] : "";
    }

    function setValue($tag, $data)
    {
        $this->meta["2#$tag"] = [$data];

        $this->write();
    }

    private function write()
    {
        $mode = 0;

        $content = iptcembed($this->binary(), $this->file, $mode);   

        $filename = $this->file;

        if(file_exists($this->file)) unlink($this->file);

        $fp = fopen($this->file, "w");
        fwrite($fp, $content);
        fclose($fp);
    }         

    private function binary()
    {
        $data = "";

        foreach(array_keys($this->meta) as $key)
        {
            $tag = str_replace("2#", "", $key);
            $data .= $this->iptc_maketag(2, $tag, $this->meta[$key][0]);
        }       

        return $data;
    }

    function iptc_maketag($rec, $data, $value)
    {
        $length = strlen($value);
        $retval = chr(0x1C) . chr($rec) . chr($data);

        if($length < 0x8000)
        {
            $retval .= chr($length >> 8) .  chr($length & 0xFF);
        }
        else
        {
            $retval .= chr(0x80) . 
                       chr(0x04) . 
                       chr(($length >> 24) & 0xFF) . 
                       chr(($length >> 16) & 0xFF) . 
                       chr(($length >> 8) & 0xFF) . 
                       chr($length & 0xFF);
        }

        return $retval . $value;            
    }   

    function dump()
    {
        echo "<pre>";
        print_r($this->meta);
        echo "</pre>";
    }

    #requires GD library installed
    function removeAllTags()
    {
        $this->meta = [];
        $img = imagecreatefromstring(implode(file($this->file)));
        if(file_exists($this->file)) unlink($this->file);
        imagejpeg($img, $this->file, 100);
    }
}

$file = "photo.jpg";
$objIPTC = new IPTC($file);

//set title
$objIPTC->setValue(IPTC_HEADLINE, "A title for this picture");

//set description
$objIPTC->setValue(IPTC_CAPTION, "Some words describing what can be seen in this picture.");

echo $objIPTC->getValue(IPTC_HEADLINE);
?>

17 hữu ích 1 bình luận chia sẻ

I have a small PHP script that grabs specific IPTC tags from an image file after uploading (see below):

move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);

$size = getimagesize('uploads/' . $_FILES['file']['name'], $info);

if(isset($info['APP13'])) { $iptc = iptcparse($info['APP13']); }

$title = $iptc["2#005"][0];
$artist = $iptc["2#025"][0];
$medium = $iptc["2#120"][0];
$date = $iptc["2#092"][0];
$credit = $iptc["2#080"][0];
$subject = $iptc["?????"][0];

All is well - except I can't for the life of me find the equivalent HEX value for the SubjectCode field! The IPTC reference documents list the value as "0x020C" - but that doesn't work. It's ID value is "2:12" according to the Adobe XMP docs. I'm stumped. Any gurus out there that can point me in the right direction?

1. Thêm code vào thẻ head là gì?

Thêm code vào giữa thẻ head trong header.php của Website WordPress đơn giản chỉ là chúng ta khai báo thêm một hoặc nhiều thẻ vào trước thẻ đóng </head>. Code ở đây có thể là thẻ html, css, font hoặc các đoạn mã nhúng  javascript, jQuery….

<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" >
    <link rel="profile" href="https://gmpg.org/xfn/11">
    <?php wp_head(); ?>
    
   <!-- Chèn code vào đây -->
</head>0

Ví dụ một đoạn mã code head html

Có nhiều cách để chèn code vào giữa thẻ <head> vào theme WordPress. Tôi sẽ hướng dẫn các bạn những cách sau đây thật chi tiết. Tùy vào trường hợp cụ thể bạn có thể lựa chọn 1 trong các cách sau:

2.1 Dùng Plugin Insert Headers and Footers

Trước tiên bạn cần phải cài plugin này vào https://vi.wordpress.org/plugins/insert-headers-and-footers/

Bạn có thể tham khảo thêm 1 số plugin cho phép nhúng code vào Header và Footer sau:

  • Elementor Header & Footer Builder
  • Elements kit Elementor addons (Header & Footer Builder, Mega Menu Builder, Layout Library)
  • Header Footer Code Manager
  • Woody code snippets – Insert Header Footer Code, AdSense Ads
  • Header Footer for Beaver Builder
  • Head, Footer and Post Injections

2.2 Sửa giao diện

Để chèn được kiểu này, quyền quản trị Website bạn phải được phép chỉnh sửa giao diện WordPress. Một số trường hợp tài khoản quản trị không được phép sử dụng quyền này. Hãy liên hệ đơn vị thiết kế website để mở quyền truy cập này.

Bước này bạn chọn đúng giao diện (theme) WordPress đang dùng nhé.

Tìm file template header.php

2.3 Sửa file header.php

Để sửa được file template header.php trước tiên bạn phải có thông tin tài khoản quản trị Hosting Website. Hầu hết các công ty thiết kế website đều giao tài khoản quản trị này cho bạn sau khi Website hoàn thành. Bạn hãy lưu lại để có lúc sẽ dùng đến.

Đăng nhập vào quản trị: (Bài viết hướng dẫn trên Hosting DirectAdmin)

2.4 Thêm bằng plugin Elementor

Trường hợp thiết kế Website kéo thả bằng Plugin Elementor, bạn có thể chèn mã vào thẻ head thông bằng plugin này như sau:

2.5 Thêm bằng code WordPress

Bạn cũng có thể thêm các thẻ meta vào giữa thẻ head thông qua action wp_head mà WordPress đã cung cấp trong file functions.php như sau:

add_action('wp_head', 'website366_add_meta');
function website366_add_meta(){
?>
    <meta name="google-site-verification" content="9WZMYhXYZMKL7RUw0abrQ1o79WTb-_sfGsgdfgsfsdfsfs" />
<?php
}

2.6 Thêm bằng theme Flatsome

Còn nếu bạn đang sử dụng theme Flatsome để thiết kế website, bạn có thể thêm các đoạn mã vào thẻ head như sau:

Những trường hợp sau bạn cần dùng đến chức năng nhúng code vào giữa thẻ head trong Website WordPress:

  • Cài đặt Google Analytics.
  • Khai báo Website với Google Search Console.
  • Nhúng mã Google Shopping quảng cáo Sản Phẩm.
  • Đo lường truy cập Facebook Pixel.

Như vậy qua bài viết này, giờ đây bạn có thể chèn dễ dàng các đoạn mã code vào thẻ head rồi phải không. Nếu gặp khó khăn gì đừng ngại liên hệ chúng tôi sẽ hỗ trợ bạn miễn phí.