Wordpress: Nasıl bir sayfa eklenmiş görüntüleri ile güncellenen bir xml dosyası tutmak gerekir?

2 Cevap php

Ben bir kullanıcı bir sayfada attched olan tüm görüntüleri ile güncel bir xml dosyası tutmak istiyorsanız, ben (çok fazla yükü neden olur çünkü) oluşturmak ve birisi o sayfayı ziyaret eden bir xml dosyası her şey üretmek yazmak gerekir - gerekir kullanıcı yerine yönetici konsolunda sayfasına yeni içerik verdiği zaman ben dosyası oluşturmak ve doldurmak?

2 Cevap

First, you will need to create directory under wp-content. Lets called it wp-content/xmlimages/ Now, copy this code and paste it to wp-content/plugins/cr-attachmentpost2xml.php:

<?php
/*
Plugin Name: CR Attachment Post To XML
Plugin URI: http://bayu.freelancer.web.id/search/plugin
Description: Automatically create XML files for each post that list post attachment of that particular post.
Version: 0.1
Author: Arief Bayu Purwanto
Author URI: http://bayu.freelancer.web.id/

*/

add_action('publish_post', 'cr_attachmentpost2xml_publish_post_hook');

//add_action('admin_menu', 'cr_attachmentpost2xml_test');

function cr_attachmentpost2xml_publish_post_hook( $post_id ){
    __cr_attachmentpost2xml_impl( $post_id );
    return $post_id;
}

function cr_attachmentpost2xml_test(){
    __cr_attachmentpost2xml_impl( 738 );
}

function __cr_attachmentpost2xml_impl( $post_id ){
    $udir = wp_upload_dir();
    $writepath = $udir['basedir'] . '/xmlimages/';
    $attachment = get_children( 'post_type=attachment&post_parent=' . $post_id);

    if(wp_is_post_revision($postId)) return $post_id;
    //print_r($attachment);
    $xml = "<?xml version=\"1.0\"?>";
    $xml .= "<attachments>";

    foreach($attachment as $att){
        $image_src = wp_get_attachment_image_src( $att->ID );
        $xml .= "<attachment>";
        $xml .= "<id>{$att->ID}</id>";
        $xml .= "<date>{$att->post_date}</date>";
        $xml .= "<title>{$att->post_title}</title>";
        $xml .= "<mime_type>{$att->post_mime_type}</mime_type>";
        $xml .= "<url>".$image_src[0]."</url>";

        $xml .= "</attachment>";
    }
    $xml .= "</attachments>";
    file_put_contents($writepath . $post_id . ".xml", $xml);
    //echo "$xml";
}

WARNING: sadece hızlı ve olduğu gibi, burada özel bir şey hariç değildir etmeyin kirli kodu. Bu istediğiniz şekilde çalışır ve bu kodu kullanabilirsiniz yaparsanız, o tanıtmak olabilecek herhangi bir güvenlik açığı için kontrol etmeyi unutmayın umuyoruz.

Bu neden ihtiyaç herhangi bir nedeni var mı? Bu veriler neden yerine istek üzerine XML döndüren basit bir API oluşturmak değil WordPress tarafından kaydedilir? Veri çoğaltarak hiçbir anlamı yok.