Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter nekrozon

    (@nekrozon)

    A slight tweak was needed for my code!

    Use this updated replace_unique_ids function for absolute control over the ENTIRE widget (version from previous post only gives you control over the body!)

    function replace_unique_ids ($pageContent, $uniqueArray)
    {
    
    	$widgetIndex = count($uniqueArray);  //Count the number of widgets that are in the sidebar we are modifying
    	$bcb = '/<div class=\"Block\">/';  //The string for a widget we are adding the unique ID to
    
    	$i = 0;  //Position of where we are in array
    	while ($i < $widgetIndex)
    	{
    		$currentID = $uniqueArray[$i]; //Get the current unique ID out of the array and into the string $currentID
    		$replaceWith = '<div id="' . $currentID . '" class="Block">';  //Construct Replacement HTML
    		$pageContent = preg_replace($bcb, $replaceWith, $pageContent, 1);  //Perform the tag replacement and limit to 1 replacement
    		$i++; //Move to next unique ID in array
    	}
    
    	return $pageContent;
    }
    Thread Starter nekrozon

    (@nekrozon)

    Well folks, I put on my PHP hat and dug into WordPress a little bit. The theme that generated this crazy code was from a WYSIWYG WordPress theme creator called Artisteer. Reference the code from my first post to see the full functions that need to be edited. Here is how I got each widget its own unique ID that WordPress Generates:

    1. Go into the Theme’s functions.php file and Edit the line of code starting with:

      if (function_exists('register_sidebars')) {
      	register_sidebars(3, array(
      		'before_widget' => '<!--- BEGIN Widget --->,

      and replace it with:

      if (function_exists('register_sidebars')) {
      	register_sidebars(3, array(
      		'before_widget' => '<!--- BEGIN Widget ---> <!-- BEGIN UNIQUE ID="%1$s" AFTER UNIQUE ID-->',

      *The change was Adding <!-- BEGIN UNIQUE ID="%1$s" AFTER UNIQUE ID--> After <!--- BEGIN Widget --->

    2. Add these two functions between art_normalize_widget_style_tokens and art_sidebar functions:

      function extract_unique_ids ($idContent)
      {
      	$uniqueIDs = array();  // Initialze Array that unqiue IDs will be stored in
      	$arrayIndex = 0;  //Counter for index of array
      
      	$beginID = '<!-- BEGIN UNIQUE ID="';  //Marks start of unique ID sequence
      	$endID = '" AFTER UNIQUE ID-->';  //Marks end of unqiue ID sequence
      	$idOffset = 0;  //Offset used for searching content
      
      	while (true)
      	{
      		$idBeginPos = strpos($idContent, $beginID, $idOffset); //Find string marking start of unique ID sequence
      		if ($idBeginPos == FALSE) break;  //If not found, break out of loop to return unique IDs
      		$idBeginPos += strlen($beginID);  //Add length of Begin Marker so position is where Unique ID starts
      		$idOffset = $idBeginPos;  //Adjust the offset to where the unqiue ID was found
      
      		$idEndPos = strpos($idContent, $endID, $idOffset);  //Find the position where the unique ID ends
      
      		$uniqueIDs[$arrayIndex++] = substr($idContent, $idBeginPos, $idEndPos - $idBeginPos);	
      
      	}
      	return $uniqueIDs;
      }
      function replace_unique_ids ($pageContent, $uniqueArray)
      {
      
      	$widgetIndex = count($uniqueArray);  //Count the number of widgets that are in the sidebar we are modifying
      	$bcb = '/<div class=\"BlockContent-body\">/';  //The string for a widget we are adding the unique ID to
      
      	$i = 0;  //Position of where we are in array
      	while ($i < $widgetIndex)
      	{
      		$currentID = $uniqueArray[$i]; //Get the current unique ID out of the array and into the string $currentID
      		$replaceWith = '<div id="' . $currentID . '" class="BlockContent-body">';  //Construct Replacement HTML
      		$pageContent = preg_replace($bcb, $replaceWith, $pageContent, 1);  //Perform the tag replacement and limit to 1 replacement
      		$i++; //Move to next unique ID in array
      	}
      
      	return $pageContent;
      }
    3. In function art_sidebar after this line of code: if (!$success) return false;
      add this line of code:
      $widgetIDs = extract_unique_ids($content);
    4. In function art_sidebar after this line of code: $content = str_replace(array_keys($replaces), array_values($replaces), $content);
      add this line of code:
      $content = replace_unique_ids($content, $widgetIDs);

    That’s it! Now why didn’t someone tell me to do that beforehand? Haha jk!

    -Nekrozon

Viewing 2 replies - 1 through 2 (of 2 total)