• Resolved Xaib Aslam

    (@lahorimela)


    Hello,
    I am trying to add a field in this code but it’s not working, can anyone help me with this?

    <?php
    include ("simple_html_dom.php");
    $name = get_post_meta($post->ID, 'company', true);
    $html = file_get_html("https://other-website-url.com/company/.$name.");
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @lahorimela
    The file_get_html function is a part of the Simple HTML DOM library, which allows you to parse and manipulate HTML documents easily. To add a field to the HTML document you’re fetching with file_get_html, you’ll need first to load the HTML into a DOM object, make the changes to that object, and then save the modified HTML. Here’s an example of how you might add a new field to the HTML document:

    <?php
    include ("simple_html_dom.php");
    $name = get_post_meta($post->ID, 'company', true);
    $html = file_get_html("https://other-website-url.com/company/".$name);
    
    // Find the element you want to add the field to
    $element = $html->find('#element-id', 0);
    
    // Create a new field
    $field = $html->createElement('input');
    
    // Set the attributes for the field
    $field->type = 'text';
    $field->name = 'new_field';
    
    // Add the field to the element
    $element->appendChild($field);
    
    // Save the modified HTML
    $html->save();
    
    // Do something with the modified HTML
    echo $html;
    ?>

    This will find the element with the ID of element-id in the HTML document and append a new input element to it with the name new_field. Please change the element-id to the one you need for the desired result.

    You should always check the website’s Terms of Use before scraping any data, as some websites may not allow web scraping.

    Thread Starter Xaib Aslam

    (@lahorimela)

    thankyou @faisalahammad

    actually i am using ACF plugin for custom fields, so what i need that i just want to display custom field here

    file_get_html("https://dps.psx.com.pk/company/".$acffield);

    i try your code but not working.

    Also i try this

    $name = get_post_meta($post->ID, 'company', true);
    $url = "https://other-website-url.com/company/";
    
    $html = file_get_html($url,$name);
    • This reply was modified 1 year, 8 months ago by Xaib Aslam.
    Thread Starter Xaib Aslam

    (@lahorimela)

    If i do this then everything is working fine. So in that case I don’t have to create multiple templates for multiple posts. 1 template for multiple posts would be easy.

    <?php
    include ("simple_html_dom.php");
    $html = file_get_html("https://site.com/company/OGDC");
    ?>
    
    
    <?php echo $html->find('.quote__sector', 0)->outertext; ?>
    
    <?php echo $html->find('.quote__price', 0)->outertext; ?>

    I just want to add the ACF field instead of this OGDC so I will add a custom field in a post and the template will pick and the URL will be complete.

    Thread Starter Xaib Aslam

    (@lahorimela)

    i figured out. Thanks @faisalahammad for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add field in file_get_html’ is closed to new replies.