pingback

Pings back the links found in a post.

Description

pingback( (string) $content, (int|WP_Post) $post_id ); 

Parameters (2)

0. $content (string)
Post content to check for links. If empty will retrieve from post.
1. $post_id (int|WP_Post)
Post Object or ID.

Usage

  1. if ( !function_exists( 'pingback' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Post content to check for links. If empty will retrieve from post. 
  5. $content = ''; 
  6.  
  7. // Post Object or ID. 
  8. $post_id = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pingback($content, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function pingback( $content, $post_id ) { 
  2. include_once( ABSPATH . WPINC . '/class-IXR.php' ); 
  3. include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); 
  4.  
  5. // original code by Mort (http://mort.mine.nu:8080) 
  6. $post_links = array(); 
  7.  
  8. $post = get_post( $post_id ); 
  9. if ( ! $post ) { 
  10. return; 
  11.  
  12. $pung = get_pung( $post ); 
  13.  
  14. if ( empty( $content ) ) { 
  15. $content = $post->post_content; 
  16.  
  17. // Step 1 
  18. // Parsing the post, external links (if any) are stored in the $post_links array 
  19. $post_links_temp = wp_extract_urls( $content ); 
  20.  
  21. // Step 2. 
  22. // Walking thru the links array 
  23. // first we get rid of links pointing to sites, not to specific files 
  24. // Example: 
  25. // http://dummy-weblog.org 
  26. // http://dummy-weblog.org/ 
  27. // http://dummy-weblog.org/post.php 
  28. // We don't wanna ping first and second types, even if they have a valid <link/> 
  29.  
  30. foreach ( (array) $post_links_temp as $link_test ) : 
  31. if ( ! in_array( $link_test, $pung ) && ( url_to_postid( $link_test ) != $post->ID ) // If we haven't pung it already and it isn't a link to itself 
  32. && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments. 
  33. if ( $test = @parse_url($link_test) ) { 
  34. if ( isset($test['query']) ) 
  35. $post_links[] = $link_test; 
  36. elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) ) 
  37. $post_links[] = $link_test; 
  38. endif; 
  39. endforeach; 
  40.  
  41. $post_links = array_unique( $post_links ); 
  42. /** 
  43. * Fires just before pinging back links found in a post. 
  44. * @since 2.0.0 
  45. * @param array &$post_links An array of post links to be checked, passed by reference. 
  46. * @param array &$pung Whether a link has already been pinged, passed by reference. 
  47. * @param int $post_ID The post ID. 
  48. */ 
  49. do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) ); 
  50.  
  51. foreach ( (array) $post_links as $pagelinkedto ) { 
  52. $pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); 
  53.  
  54. if ( $pingback_server_url ) { 
  55. @ set_time_limit( 60 ); 
  56. // Now, the RPC call 
  57. $pagelinkedfrom = get_permalink( $post ); 
  58.  
  59. // using a timeout of 3 seconds should be enough to cover slow servers 
  60. $client = new WP_HTTP_IXR_Client($pingback_server_url); 
  61. $client->timeout = 3; 
  62. /** 
  63. * Filters the user agent sent when pinging-back a URL. 
  64. * @since 2.9.0 
  65. * @param string $concat_useragent The user agent concatenated with ' -- WordPress/' 
  66. * and the WordPress version. 
  67. * @param string $useragent The useragent. 
  68. * @param string $pingback_server_url The server URL being linked to. 
  69. * @param string $pagelinkedto URL of page linked to. 
  70. * @param string $pagelinkedfrom URL of page linked from. 
  71. */ 
  72. $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . get_bloginfo( 'version' ), $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom ); 
  73. // when set to true, this outputs debug messages by itself 
  74. $client->debug = false; 
  75.  
  76. if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered 
  77. add_ping( $post, $pagelinkedto );