viernes, 12 de septiembre de 2014

Publicar en WordPress desde Microsoft Access via XMLRPC Parte 5

Quinta entrega! Estamos investigando cómo conectar Microsoft Access con WordPress. En esta quinta entrega vamos a completar campos personalizados de un tipo de contenido vía XMLRPC. Recomiendo que leas las anteriores:

Parte 4: custom content type


Cambios a nuestro sitio de WordPress

El primera paso es agregar un plugin que nos facilite la creación de campos personalizados para nuestro tipo de contenido "proceso". Instalamos: Advanced Custom Fields.


Luego agregamos tres campos nuevos:
  • Número de certificado
  • Cliente
  • Documento


Vemos como aparecen estos nuevos campos en el momento de editar un "proceso":



El código

Sub PublicarEnWordPress()
    ' Datos de Autenticación
    CargarConstantes
    txtURL = SITIO
    txtUserName = USUARIO
    txtPassword = PASSWORD
    
    ' Datos del Post
    txtTitulo = "Certificado 6"
    txtContenido = "Este es un custom post de ejemplo publicado desde Microsoft Access"
    Dim txtCategorias(1) As String
    txtCategorias(1) = "Uncategorized"
    txtImagen = "25"
    txtTipoContenido = "proceso"
    txtCliente = "Cliente 1"
    txtNumeroCertificado = "APP-218539"
    txtDocumento = "25"
      
    ' ServerXMLHTTP
    Dim objSvrHTTP As ServerXMLHTTP
    Dim strT As String
    Set objSvrHTTP = New ServerXMLHTTP
  
    ' Autenticación
    objSvrHTTP.Open "POST", txtURL, False, CStr(txtUsuario), CStr(txtPassword)
    objSvrHTTP.setRequestHeader "Accept", "application/xml"
    objSvrHTTP.setRequestHeader "Content-Type", "application/xml"

    strT = strT & "<methodCall>"
    
    ' Acción
    strT = strT & "<methodName>wp.newPost</methodName>"
    
    ' General
    strT = strT & "<params>"
    strT = strT & "<param><value><string>0</string></value></param>"
    strT = strT & "<param><value>" & txtUserName & "</value></param>"
    strT = strT & "<param><value><string>" & txtPassword & "</string></value></param>"
    strT = strT & "<param>"
    
    strT = strT & "<struct>"
    
    ' Categorías
    strT = strT & "<member><name>categories</name><value><array>"
    strT = strT & "<data>"
    
    For i = 1 To UBound(txtCategorias)
        strT = strT & "<value>" & txtCategorias(i) & "</value>"
    Next i
    strT = strT & "</data>"
    strT = strT & "</array></value></member>"
    
    ' Título, contenido y fecha
    strT = strT & "<member><name>post_content</name><value><![CDATA[" & txtContenido & "]]></value></member>"
    strT = strT & "<member><name>post_title</name><value>" & txtTitulo & "</value></member>"
    strT = strT & "<member><name>dateCreated</name><value><dateTime.iso8601>" & Format(Now(), "yyyyMMdd") & "T" & Format(Now(), "hh:mm:ss") & "</dateTime.iso8601></value></member>"
    
    ' Imagen destacada
    strT = strT & "<member><name>post_thumbnail</name><value><![CDATA[" & txtImagen & "]]></value></member>"
    
    ' Tipo de contenido
    strT = strT & "<member><name>post_type</name><value>" & txtTipoContenido & "</value></member>"
    
    ' Campos personalizados
    strT = strT & "<member><name>custom_fields</name><value><array><data><value>"
    
    strT = strT & "<struct>"
    strT = strT & "<member><name>key</name><value>cliente</value></member>"
    strT = strT & "<member><name>value</name><value>" & txtCliente & "</value></member>"
    strT = strT & "</struct>"
    
    strT = strT & "<struct>"
    strT = strT & "<member><name>key</name><value>numero_de_certificado</value></member>"
    strT = strT & "<member><name>value</name><value>" & txtNumeroCertificado & "</value></member>"
    strT = strT & "</struct>"
    
    strT = strT & "<struct>"
    strT = strT & "<member><name>key</name><value>documento</value></member>"
    strT = strT & "<member><name>value</name><value>" & txtDocumento & "</value></member>"
    strT = strT & "</struct>"
    
    strT = strT & "</value></data></array></value></member>"
    
    ' Tipo de publicación
    strT = strT & "<member><name>post_status</name><value>publish</value></member>"
    
    strT = strT & "</struct>"
    strT = strT & "</param>"
    strT = strT & "</params>"
    strT = strT & "</methodCall>"
    
    ' Publicación
    objSvrHTTP.send strT

    ' Debug
    Debug.Print objSvrHTTP.responseText
    MsgBox objSvrHTTP.responseText

End Sub


La publicación

Vemos en esta imagen cómo se cargaron los campos personalizados:



Bibliografía


Nos vemos en la parte 6!!!

No hay comentarios:

Publicar un comentario