• Hey guys,

    I have downloaded https://simplehtmldom.sourceforge.net/ library into my wp-content. Now I want to include this file into my function in functions.php.

    But I dont know what kind of URL I should use here. Please, can you give me an advice? The code:

    function foobar_func( $atts ){
    require_once('URL path?/simple_html_dom.php');
    $html = file_get_html('https://hokejbal.cz/1-liga/tabulky/');
    $ret = $html->find('table.standings', 1);
    print $ret;
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    a “require” statement uses a file path, not a URL.

    Thread Starter ragulin

    (@ragulin)

    ok, so it would be the FTP path?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    It would be a relative path to the location of functions.php.

    Hi ragulin,

    The quickest way to do an include of that library is thru your theme’s function.php, some suggested steps to do that:
    1) Copy the files https://simplehtmldom.sourceforge.net/ inside your theme’s folder eg simplehtmldom
    2) Inside your theme’s function.php file, you can use this modified code to include that 3rd party library

    function foobar_func( $atts ){
    require_once('simplehtmldom/simple_html_dom.php');
    $html = file_get_html('https://hokejbal.cz/1-liga/tabulky/');
    $ret = $html->find('table.standings', 1);
    print $ret;
    }

    3) You can now call the foobar_func in any of your template files.

    The best way to do this is creating a plugin (Google for it) so your custom code won’t be lost when you switch themes.

    Thread Starter ragulin

    (@ragulin)

    Okay, my code in functions.php

    
    function foobar_func( $atts ){
    	require_once('ftp://[email protected]/www/wp-content/themes/sporty-child/simple_html_dom.php');
    	$html = file_get_html('https://hokejbal.cz/1-liga/tabulky/');
    	$ret = $html->find('table.standings', 1);
    	print $ret;
    	
    }
    add_shortcode( 'foobar', 'foobar_func' );

    And errors I get when I try to call the function via shortcode in my page

    
    Warning: require_once(): ftp:// wrapper is disabled in the server configuration by allow_url_include=0 in /web/htdocs2/skkelticz/home/www/wp-content/themes/sporty-child/functions.php on line 11
    
    Warning: require_once(ftp://[email protected]/www/wp-content/themes/sporty-child/simple_html_dom.php): failed to open stream: no suitable wrapper could be found in /web/htdocs2/skkelticz/home/www/wp-content/themes/sporty-child/functions.php on line 11
    
    Fatal error: require_once(): Failed opening required 'ftp://[email protected]/www/wp-content/themes/sporty-child/simple_html_dom.php' (include_path='.') in /web/htdocs2/skkelticz/home/www/wp-content/themes/sporty-child/functions.php on line 11
    • This reply was modified 7 years, 7 months ago by Steven Stern (sterndata). Reason: put code in backticks
    • This reply was modified 7 years, 7 months ago by ragulin.
    Dion

    (@diondesigns)

    Most servers (including yours) disable the ability to use URL wrappers (ftp://, https://, etc) in the include() and require() statements.

    I’d suggest putting your “simplehtmldom” directory in the WordPress root, where wp-config.php is located. You can then include your file in WordPress with the following line:

    require_once(ABSPATH . 'simplehtmldom/simple_html_dom.php');

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘acessing PHP library in functions.php’ is closed to new replies.